android 学习摘要

简介: 引用:http://kyfxbl.iteye.com/blog/1208011 1、内置应用和开发的应用,没有区别 2、Linux Kernel --> Dalvik VM --> Applications 3、Kernel: 硬件抽象层,同时提供了进程、内存、文件系统管理等核心服务    Run...

引用:http://kyfxbl.iteye.com/blog/1208011

1、内置应用和开发的应用,没有区别 

2、Linux Kernel --> Dalvik VM --> Applications 

3、Kernel: 硬件抽象层,同时提供了进程、内存、文件系统管理等核心服务 

   Runtime: Dalvik VM,运行时环境 

   Code libraries: 浏览器、数据库、绘图、影音 

   Managers: Activities、Views、Telephony等管理器 

   Hardware --> Linux Kernel --> Runtime & Libraries --> Application Managers --> Applications 

4、Android基于Linux Kernel,运行在Dalvik VM里 

5、四种核心组件:Activity、Service、Provider、Receiver 

6、A best practice is to launch Services on a periodic or as-needed basis, triggered by a system alarm, and then have the Service terminate when its task is complete 

7、在manifest.xml文件里注册的Receiver组件,不需要应用启动,就可以被触发 

8、每个Android Application跑在一个单独的进程里 

9、在OS资源短缺的时候,Application所在的进程,有可能会被kill,不同状态的进程的优先级有区别 

10、android应用不是跑在jvm里,而是跑在Dalvik VM里,所有java字节码要转换成.dex文件格式 

11、strings这些资源也是被编译成binary 

12、You can define layout and views directly in code or in a layout XML resource file 

13、you don’t have to use an XML file at all; instead, you can define all your layout and View configuration directly in code, as Java objects. This technique is used in applications where a dynamic GUI is required. Generally speaking, it’s often easier (and better practice) to use an XML layout resource for each Activity. An XML layout file defines View objects, organized into a hierarchical tree structure. After they’re defined in relation to the parent layout,each view can then be inflated at runtime 

14、Android employs a handy adapter concept used to link views that contain collections with an underlying data source 

15、An Adapter is a collection handler that returns each item in the collection as a View 

16、Every process running on the Android platform is placed on a stack. When you use an Activity in the foreground, the system process that hosts that Activity is placed at the top of the stack, and the previous process (the one hosting whatever Activity was previously in the foreground) is moved down one notch 

17、It decides which ones to get rid of based on a simple set of priorities: 
1 The process hosting the foreground Activity is the most important. 
2 Any process hosting a visible but not foreground Activity is next in line. 
3 Any process hosting a background Activity is next in line. 
4 Any process not hosting any Activity (or Service or BroadcastReceiver) is known as an empty process and is last in line. 

18、adb shell dumpsys activity 

19、If the process your Activity is in falls out of the foreground, it’s eligible to be killed and it’s not up to you; it’s up to the platform’s algorithm, based on available resources and relative priorities 

20、 
1 onCreate() 
Called when the Activity is created. Setup is done here. Also provided is access to any previously stored state in the form of a Bundle 
2 onRestart() 
Called if the Activity is being restarted, if it’s still in the stack, rather than starting new 
3 onStart() 
Called when the Activity is becoming visible on the screen to the user 
4 onResume() 
Called when the Activity starts interacting with the user (This method is always called, whether starting or restarting) 
5 onPause() 
Called when the Activity is pausing or reclaiming CPU and other resources. This method is where you should save state information so that when an Activity is restarted, it can start from the same state it was in when it quit 
6 onStop() 
Called to stop the Activity and transition it to a nonvisible phase and subsequent 
lifecycle events 
7 onDestroy() 
Called when an Activity is being completely removed from system memory. This method is called either because onFinish() is directly invoked or the system decides to stop the Activity to free up resources 

21、it’s important to know that onPause() is the last opportunity you have to clean up and save state information. The processes that host your Activity classes won’t be killed by the platform until after the onPause() method has completed, but they might be killed thereafter 

22、The system will attempt to run through all of the lifecycle methods every time, but if resources are spiraling out of control, as determined by the platform, a fire alarm might be sounded and the processes that are hosting activities that are beyond the onPause() method might be killed at any point 

23、In addition to persistent state, you should be familiar with one more scenario: instance state. Instance state refers to the state of the UI itself. The onSaveInstanceState()method is called when an Activity might be destroyed, so that at a future time the interface state can be restored 

24、耗时比较多的工作,应该在UI Thread之外完成,实现的方法是使用Handler类 

25、android支持自定义View组件 

26、In Android, screen layout is defined in terms of ViewGroup and 
LayoutParams objects. ViewGroup is a View that contains other views (has children) and also defines and provides access to the layout

 

--------------------------------------------------------------------------

引用:http://kyfxbl.iteye.com/blog/1240746

一、Each Activity can make an Intent call to get something done without knowing exactly who’ll receive that Intent 
Android的这种调用方式,是一种各组件之间低耦合的集成方式。比如自己的应用需要调用打电话的功能,可以创建一个Intent.setAction(DIAL),请求“某一个”电话程序来完成打电话功能。如果没有安装第三方的程序,那么通常手机自带的电话程序会响应这个请求,如果安装了第三方的拨号程序(当然这种情况很少),那么就会由第三方拨号程序完成通话。而原来的Activity,对这一切是一无所知的 

二、用intent来描述意图,然后用intent-filter来进行全局匹配。如果找到唯一结果,就进行调用;如果找到多个结果,则要求用户选择;如果没有找到结果,则抛出异常。手机上安装的所有程序,无论是自带的,还是第三方应用,都会参与这个匹配过程。当然,如果在manifest.xml中没有设置intent-filter,就不会参与到intent匹配。这种情况下,这种组件就只能通过显式调用被调用到 

三、 
Action:     Fully qualified String indicating the action (for example, android.intent.action.DIAL) 
Category:   Describes where and how the Intent can be used, such as from the main 
            Android menu or from the browser 
Component:  Specifies an explicit package and class to use for the Intent, instead of inferring from action, type, and categories 
Data:       Data to work with, expressed as a URI (for example, content://contacts/1) 
Extras:     Extra data to pass to the Intent in the form of a Bundle 
Type:       Specifies an explicit MIME type, such as text/plain or vnd.android.cursor.item/email_v2 

四、implicit and explicit invocation。这个说的是隐式调用和显式调用 

五、Android parses each <intent-filter> element into an IntentFilter object. After Android installs an .apk file, it registers the application’s components, including the Intent filters. When the platform has a registry of Intent filters, it can map any Intent requests to the correct, installed Activity, BroadcastReceiver, or Service 

六、下面是intent-filter的匹配规则 

Action: Each individual IntentFilter can specify zero or more actions and zero or more categories. If the action isn’t specified in the IntentFilter, it’ll match any Intent; otherwise, it’ll match only if the Intent has the same action 

Category: An IntentFilter with no categories will match only an Intent with no categories; otherwise, an IntentFilter must have at least what the Intent specifies. For example,if an IntentFilter supports both the HOME and the ALTERNATIVE categories, it’ll match an Intent for either HOME or CATEGORY. But if the IntentFilter doesn’t provide any categories, it won’t match HOME or CATEGORY 

Data: scheme://authority/path分别对应android:schema、android:host、android:path 

七、A broadcast Intent doesn’t invoke an Activity, so your current screen usually remains in the foreground 

八、Android best practices discourage long-running services. Services that run continually and constantly use the network or perform CPU-intensive tasks will eat up the device’s battery life and might slow down other operations 

九、In Android, each application runs within its own process. Other applications can’t directly call methods on some service of another application, because the applications are in different sandboxes 

十、If you want to allow other developers to use your weather features, you need to give them information about the methods you provide, but you might not want to share your application’s source code. Android lets you specify your IPC features by using an interface definition language (IDL) to create AIDL files 

十一、Service lifecycle 

SERVICE-STARTED LIFECYCLE: If you start a Service by calling Context.startService(), it runs in the background whether or not anything binds to it. If the service hasn’t been created, the Service onCreate() method is called. The onStart() method is called each time someone tries to start the service, whether or not it’s already running. Additional instances of the Service won’t be created. The Service will continue to run in the background until someone explicitly stops it with the Context.stopService() method or when the Service calls its own stopSelf() method. You should also keep in mind that the platform might kill services if resources are running low, so your application needs to be able to react accordingly 

SERVICE-BOUND LIFECYCLE: If an Activity binds a Service by calling Context.bindService(), it’ll run as long 
as the connection is open. An Activity establishes the connection using the Context and is also responsible for closing it. When a Service is only bound in this manner and not also started, its onCreate() method is invoked, but onStart() is not used. In these cases, the platform can stop and clean up the Service after it’s unbound 

CLEANING UP WHEN A SERVICE STOPS: When a Service stops, its onDestroy() method is invoked. Inside onDestroy(), every Service should perform final cleanup, stopping any spawned threads, terminating network connections, stopping services it had started, and so on

------------------------------------------------------------------------------------------------------------

 

引用:http://www.iteye.com/topic/1117799

1、Activity和Service组件是Context的子类,BroadcastReceiver和ContentProvider不是 

2、用adb shell命令,可以看到底层linux的目录结构 
data/anr 记录ANR信息 
data/app/*.apk 应用的安装源文件 
data/data/package_name 装好的应用会出现在这里,package_name取决于Manifest.xml文件中的定义 
data/data/package_name/shared_prefs/name.xml SharedPreferences文件会保存在这里 
data/data/package_name/files/file.name 用openFileOnput()创建的文件在这里 
data/data/package_name/databases/dbname.db 内置的SQLite数据库文件在这里,如果没有这个文件,就会触发SQLiteOpenHelper类的onCreate()方法 

3、利用SharedPreferences来存储键值对 
   Context.MODE_PRIVATE 0 
   Context.MODE_WORLD_READABLE 1 
   Context.MODE_WORLD_WRITEABLE 2 
创建的文件在这个路径:shared_prefs/name.xml 

4、用Context的createPackageContext()方法,可以得到另外一个应用的Context,不过前提是你得知道另外一个应用的package_name,但是别人如果不告诉你,你一般是不会知道的 
Context.createPackageContext(String packageName,Context.MODE_WORLD_READABLE) 

5、SharedPreferences are ultimately backed by XML files on the Android filesystem 

6、Context.openFileOutput(String fileName,Context.MODE_PRIVATE); 
创建的文件保存在files/file.name 

7、Context.openFileInput(String fileName,Contet.MODE_PRIVATE); 

8、Occasionally, setting the user ID of your application can be extremely useful. For instance, if you have multiple applications that need to share data with one another, but you also don’t want that data to be accessible outside that group of applications, you might want to make the permissions private and share the UID to allow access. You can allow a shared UID by using the sharedUserId attribute in your manifest: android:sharedUserId="YourID". 

9、When you place a file in the res/raw location, it’s not compiled by the platform, but is available as a raw resource 

10、Files in res/xml are different from raw files in that you don’t use a stream to access them because they’re compiled into an efficient binary form when deployed. 

11、The SD card is removable, and SD card support on most devices (including Android-powered devices) supports the File Allocation Table (FAT) ) filesystem. The SD card doesn’t have the access modes and permissions that come from the Linux filesystem. 

12、要用好Android内置的SQLite,首先需要熟练掌握下面4个类的用法 
SQLiteOpenHelper 
SQLiteDatabase 
ContentValues 
Cursor 

13、Unlike the SharedPreferences you saw earlier, you can’t make a database WORLD_READABLE. Each database is accessible only by the package in which it was created. If you need to pass data across processes, you can use AIDL/Binder or create a ContentProvider, but you can’t use a database directly across the process/package boundary. 

14、内置的SQLite数据库,可以用 
adb shell 
cd data/data/package_name/databases 
sqlite3 name.db 
来访问,在这个命令行界面,可以使用很多命令,有一个比较方便的是.header ON,可以在用select语句的时候,打印出列明。不然什么都看不到,因为默认的是.header OFF 

15、ContentProvider可以跨应用访问数据库 

16、用完Cursor之前,不能调用SQLiteDatabase.close();否则Cursor就失效了,为了这个低级错误,浪费了很多时间调试代码。。。不过最终写了一个还算不错的通用数据库访问层,可以支持简单的ORM和跨表事务。代码在公司,周一再专门写一篇博客发出来 

17、SQLiteDatabase.insert()中用到的ContentValues不包含autoincrement的_id,这个也是低级错误。。也研究了好长时间 

18、单元测试继承自AndroidTestCase,里面有一个getContext() 

19、MainActivity.this,这个一般常出现在OnClickListener的onClick()方法体里,因为OnClickListener在这个场景里常常是作为一个内部类出现,所以MainActivity.this,指的就是所在的MainActivity对象的引用 

20、Cursor用完要关闭哦,亲,不是只关DB就可以的 

21、如果一个Receiver要同时侦听好多事件,可以在onReceive()方法里用intent.getAction()来判断触发的是哪个事件 

22、侦听网络变化用ConnectivityManager,而不是TelephonyManager,因为可能会捕获到2次OFF_LINE事件 
Java代码 
  收藏代码
  1. public class NetworkChangeReceiver extends BroadcastReceiver {  
  2.   
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.   
  6.         ConnectivityManager cm = (ConnectivityManager) context  
  7.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
  8.   
  9.         NetworkInfo status = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
  10.         if (status.isConnected()) {  
  11.             Log.i("tag""网络可用");  
  12.         } else {  
  13.             Log.i("tag""网络不可用");  
  14.         }  
  15.   
  16.     }  
  17.   
  18. }  

Xml代码 
  收藏代码
  1. <application android:icon="@drawable/icon" android:label="@string/app_name">  
  2.       
  3.         <receiver android:name=".receiver.NetworkChangeReceiver">  
  4.             <intent-filter>  
  5.                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />  
  6.             </intent-filter>  
  7.         </receiver>  
  8.   
  9.     </application>  
  10.       
  11.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
 
相关文章
|
6月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
155 0
|
6月前
|
安全 Linux Android开发
Android安全启动学习(一):AVB校验是什么?
Android安全启动学习(一):AVB校验是什么?
422 0
|
6月前
|
存储 安全 Linux
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
334 0
|
2月前
|
Java Maven 开发工具
第一个安卓项目 | 中国象棋demo学习
本文是作者关于其第一个安卓项目——中国象棋demo的学习记录,展示了demo的运行结果、爬坑记录以及参考资料,包括解决Android Studio和maven相关问题的方法。
第一个安卓项目 | 中国象棋demo学习
|
1月前
|
Web App开发 编解码 视频直播
视频直播技术干货(十二):从入门到放弃,快速学习Android端直播技术
本文详细介绍了Android端直播技术的全貌,涵盖了从实时音视频采集、编码、传输到解码与播放的各个环节。文章还探讨了直播中音视频同步、编解码器选择、传输协议以及直播延迟优化等关键问题。希望本文能为你提供有关Andriod端直播技术的深入理解和实践指导。
48 0
|
6月前
|
监控 Unix 应用服务中间件
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
|
2月前
|
Android开发
Android学习 —— 测试init.rc中的条件触发的处理顺序
Android学习 —— 测试init.rc中的条件触发的处理顺序
|
3月前
|
搜索推荐 Android开发
学习AOSP安卓系统源代码,需要什么样的电脑?不同配置的电脑,其编译时间有多大差距?
本文分享了不同价位电脑配置对于编译AOSP安卓系统源代码的影响,提供了从6000元到更高价位的电脑配置实例,并比较了它们的编译时间,以供学习AOSP源代码时电脑配置选择的参考。
261 0
学习AOSP安卓系统源代码,需要什么样的电脑?不同配置的电脑,其编译时间有多大差距?
|
6月前
|
存储 定位技术 开发工具
Android 开发前的设计,Android之内存泄漏调试学习与总结
Android 开发前的设计,Android之内存泄漏调试学习与总结
|
6月前
|
Java Android开发 Dart
50家大厂面试万字精华总结android编程基础学习
50家大厂面试万字精华总结android编程基础学习