Original link http://sparkyuan.me/2016/03/14/Four components of the work process/reprint annotated source
The system of the four components of the process of a large degree of encapsulation, daily development does not need to understand the underlying principle of work, then the significance of the study of these principles where?
- If you want to be more technical, then it is necessary to understand how some systems work, as well as the technical capabilities that developers will need to grow into senior engineers in the future.
- Android as a good Linux-based operating system, there must be a lot of things we learn from the internal, through the study of the Android operating system to improve the strength of developers have great benefits.
- If you are engaged in the development of Android ROM, then there is nothing to say, see the source bar.
This paper focuses on the four components of the work process analysis, through the analysis of their work process to understand the system internal operating mechanism, deepen our understanding of the overall Android system structure.
The UML diagram in this article is from @amurocrash, thanks to Amurocrash.
Android related parts of the source is too much, all pasted too cumbersome, so borrowed Amurocrash classmate's UML diagram makes the whole process easier to understand.
Operational status of four components
- The main function of activity is to display an interface and interact with the user, which plays a role in the foreground interface.
- A service is a computational component that performs a series of computational tasks in the background. The service has two states: the start State and the binding state. The service in the startup state does not need to interact with the outside world, and the service of the bound state can easily communicate with the service component. The service is running in the main thread, so time-consuming background computations still need to be done in a separate thread. Flexible adoption of both StopService and Unbindservice methods to completely stop a service component.
- Broadcastreceiver is a message-type component that is used to deliver messages between different components and even different applications. Broadcast registration has both static and dynamic methods, dynamic registration through Context.registerreceiver () to achieve, do not need to contex.unregisterreceiver () to de-broadcast, this way must be applied to start to register Static registration is performed in the Androidmanifest file, which is parsed by the system when the application is installed, and can receive broadcasts without starting the application. The matching process is described by.
- ContentProvider is a shared component that is used to share data with other components and even other applications. It maintains a data set internally, and needs to implement additions and deletions to these four operations, which can be implemented either through a database or by other types, such as List,map. It should be noted that additions and deletions to handle the thread synchronization, these methods are called in the binder thread pool, in addition, contentprovider do not need to stop manually.
The working process of the activity
Note
- The real implementation of the start activity is done by the Activitymanagernative.getdefault (). StartActivity method. This method returns Activitymanagerservice.
- Activitymanagerservice (AMS) inherits from Activitymanagernative, And Activitymanagernative inherits from Binder and implements the binder interface of Iactivitymanager, so AMS is also a binder.
- AMS This binder object is provided in a singleton mode, and the first call to its Get method is initialized with the Create method, and the previously created object is returned directly in subsequent calls.
- As you can see from the implementation of Makeapplication, if application has been created, it will not be created again, which means that an application has only one application object. The creation of application is also done through instrumentation, which is implemented by the ClassLoader, just as the activity object is created.
- Contextimpl is the specific implementation of the context, Contextimpl is through the activity of the attach method to relate to the activity, In the Attach method, the activity also completes the creation of the window and establishes its own association with the window, so that when the activity receives the event it can be passed to window.
The work process of the service
startup process
Binding process
Note
-The service has two states: the start State and the binding state, and the two states can coexist.
The working process of broadcastreceiver
Broadcastreceiver's work process includes the broadcast registration process, the broadcast sending and receiving process.
Dynamic registration
Send Receive
Note:
- Static registration is performed by Packagemanagerservice (PMS) when the application is installed, and in addition to broadcasting, the other three components are parsed and registered by the PMS when the application is installed.
- There are several types of broadcasts: Normal broadcast, ordered broadcast, and sticky broadcast, ordered broadcast and sticky broadcasts have different characteristics than ordinary broadcasts, but the sending and receiving processes are similar.
- Flag_include_stopped_packages: Broadcasts are sent to apps that have stopped, flag_exclude_stopped_packages broadcasts are not sent to apps that have stopped
- Starting with Android 3.1, apps that are in a stopped state cannot accept a power-on broadcast.
ContentProvider
startup process
- When the ContentProvider process is started, it will be launched at the same time and released into the AMS, it should be noted that this time its oncreate to application OnCreate execution, which is a rare phenomenon in the four components.
- With the starting entry as the main method of Activitythread, the main method creates the Activitythread instance and creates the main thread message queue.
- The Attach method remotely invokes the Attachapplication method of AMS and provides applicationthread for communication with AMS.
- The Attachapplication method uses the Bindapplication method and H to recall the handlebindapplication of Activitythread, which first creates application, The ContentProvider is reloaded before the application OnCreate method is recalled.
- The Multiprocess property of the ContentProvider determines whether the ContentProvider is a singleton (false), and generally uses a single case.
- The specific class of Ontentresolver is Applicationcontentresolver, when the ContentProvider process is not started, the first access to it triggers the creation of contentprovider and the process starts.
Query process
Insert,delete is similar to the Update method and is not being analyzed here.
Original link http://sparkyuan.me/2016/03/14/Four components of the work process/reprint annotated source
Understanding the working process of four components from source point of view--android developing art exploration notes