本文主要是Inside the Android Application Framework的随笔记录。
Managed Component Lifecycle
An Android APK is a collection of components
Databases, preferences, file space, etc.
Also: a Linux process. (一个App占用一个进程空间)
Every Android component has a managed lifecycle
今天听别人说iOS只有一个线程是Active的,当用户切换到另外一个应用后,原来的应用停止了,不像Android会继续在后台运行,求证!
Activities, Tasks, Processes
Task是一个跨进程的概念,可以将它理解为实现特定应用的全部组件集合。一个apk可能占用多个进程,可以在AndroidManifest中指定。进程的概念是操作系统级别的,做Android应用的视角主要集中在四个基本组件上:Activity、Service、Broadcast Receiver和Content Provider。
Activities:
Task:
Process:
Process Lifecycle
Activity Lifecycle
Activity的生命周期很低地方都能找到。
Threading
线程主要包含三种:一是主线程(UI线程),二是界面无关的子线程,三是界面相关的子线程。三种线程分别对应着ActivityThread、HanderThread和AsyncTask。
Looper
Views:
Service Lifecycle
Process Resource Management
Spawned by the special “Zygote” process
Process + pre-warmed Dalvik VM == responsiveness Zyote进程空间中包含Dalvik VM的代码,fork新进程时可以共享。
Process runs under user ID unique to system
Process + User ID == security 每个进程在应用安装时都会分配一个Linux用户ID,标识应用程序身份
Process Secutiry
Each application is given a unique user ID
No exceptions!
...except these: init, Zygote, and the main runtime
Each application has direct access only to its own data
Other apps’ resources are available only via defined, explicitly-exposed APIs
i.e. Issuing Intents, binding to Services or ContentProviders
IPC
Binder
Parcelable
Bundles
Parcel意思是打包,相当于Java的序列化,它是通过Binder进行RPC必要的数据传输手段。Parcel和Bundle不能不好区分,可以用Android代码的注释来解释下它们的区别:
Bundle:A mapping from String values to various Parcelable types.
Parcel:Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the general interface), and references to live objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.
AIDL“Android Interface Definition Language”
The Activity Manager manages the lifecycle of the applications and provides a common navigation backstack of applications that are running in dierent processes.
The Package Manager maintain track of all applications that are installed in the device.
The Windows Manager manages screen of mobile device and creates surfaces (an allocated memory block) for all running applications.
The Telephony Manager support applications to access the information regarding telephony services. Access to some telephony
information is protected and some applications may not have permission to access such protected information (Constrain to permissions declared in its manifest le).
Content Providers supports the sharing and accessing of data among applications; suppose the messaging service is an application that can access the data of other application contacts. In Android the term resources refers to the non code external assets.
The Resource Manager provides access to such external resources to native Android application at built time. Android supports a number of different kinds of resource files, including XML (use to store anything, other than bitmaps and Raw), Bitmap (Use to store images), and Raw files (other resources such as sound, string, etc).
The development of new Android applications requires access to views of previously built application. The View System provides access to some extensible set of views, such as lists, text boxes, grids, embedded browser, and button.
Notication Manager allows application to notify users about events that take place. The notication can be inform of vibration, flashing LEDs of mobile device, playing specific sound against specific event, or could be an icon (persistently displayed into the status bar).