One, process model and process hosting
1, an app is made up of one or more components that can run in a process or run in multiple processes, and the system is solely responsible for the process's construction and destruction. 2, an application process has only one application environment object, which is constructed before the component of the first application process is loaded, and destroyed after the last component in the application process finishes. 3, the component can be configured to the specified process through android:process = ": Com.zy.tool.another", and the colon begins by indicating that this is a private process that only the components of this app can use. If the start of a lowercase letter is a shared process, then other app components can also use the process. So an app can have one or more processes, that is, you can have a default process and multiple additional processes. 4, process by priority from high to low is divided into: 1) foreground process: There are interface components that are interacting with the user or interface components that have service components and user interaction or oncreate that have serviced components, OnStart, OnDestroy methods are executing or have a foreground service component or a trigger component The OnReceive method is executing. 2) Visual process: There is no interface component that interacts with the user, but the interface component is visible, or the service is bound to a user-visible interface component. 3) Service process: There are services running in the background. 4) Background process: There are no interface components that interact with the user, and there is no visible interface component, that is, the entire app is in the background and there are no running services. 5) Empty process: No components are running in the process. The process priority is constantly changing, and it changes as the component and user interaction state changes in the process. Process Recycling Policy:1) Recovery by priority from low to high. 2) The more recently used processes are recycled later. Behavior that could cause the process to be recycled: 1) The interface component is destroyed. 2) The trigger execution is complete or is logged off. 3) The system enters the standby state. 5 exception processes and unresponsive processes: 1) Exception process: The system will first save the contents of the interface component stack for each task in the process, and then forcibly terminate the process. The new application process is then reconstructed and the component stacks of each task are restored to the state before the exception. 2) unresponsive process: The app has more than 5 seconds for the user to process or the trigger's onreceive execution time exceeds 10 seconds. A popup dialog box prompts the user to terminate the process or to continue waiting.
two, the life cycle of the component:
1, the life cycle of the interface component:1) The OnCreate method is called first when the component is constructed, and OnStart () enters the visual state after execution. 2) If it is a restorative construct (that is, the interface component is reclaimed by the system and not the user), then Onrestoreinstancestate () is called. 3) Onresume () to enter the foreground state after execution, can interact with the user. 4) If the interface component is reclaimed by the system, the Onsaveinstancestate () method is executed. 5) OnPause () The interface component enters the visual state after execution. Note: If the system is forced to recycle, then the next two methods may be too late to execute. 6) OnStop () After executing the interface component into the background state. 7) OnDestroy () The end of the life cycle of the interface component after execution.
2, service component life cycle:Reference: A in-depth Android core components (1): Http://mobile.51cto.com/android-315615.htm B Android Component series, Android Service deep parsing:
Http://www.cnblogs.com/smyhvae/p/4070518.html
1) The service is run in the main thread by default, and the background in Android means that the runtime does not depend on the UI, even if the app is turned off, and the service can continue to run as long as the process is in progress.
2) Once the service is started, it is in a running state, and only the call to the StopService or Stopself method will stop the service.
3) The first start of the service by means of a call executes the OnCreate method and then executes the Onstartcommand method, and if the service does not stop, calling the service directly executes the Onstartcommand method.
4) The return value of the Onstartcommand method of the service:
A start_sticky: After the service process is killed, subsequent if the resource runs the system restarts the service and executes the Onstartcommand method, it does not retain the intent object that was previously transferred unless the component is resent.
For example, a music app that returns this value to a service to manually invoke code to shut down the service at the appropriate time.
b The Start_redeliver_intent system will automatically restart the service and will pass the INTENT in. after re-sending, the value of the flags parameter becomes Service.flag_start_redelivery
C Start_not_sticky: The service will not start again after it is terminated.
5) Intentserver is an asynchronous, automatically shut down service that executes in a separate sub-thread, and the service will only start one thread at a time to perform the task, and multiple tasks will be queued and executed sequentially. Onhandleintent the service stops automatically after execution, the OnDestroy method is called.
6) The service must be destroyed (executing the OnDestroy method) without being associated with other components (calling the Unbindservice method) and stopped (calling StopService), disassociate or stop the service separately, and will not destroy the services.
7) Bind mode start Service: Bindservice method is asynchronous, is called immediately returns, does not return IBinder object, to receive IBinder object, must implement a serviceconnection instance, And pass the instance to the Bindservice method, the system will call the Onbind method first, the return value of the method is a IBinder object, and then callback Serviceconnection onserviceconnected method, and pass the IBinder object through the arguments, and then we can call the IBinder method in the method.
Services initiated in this way, we typically create an inner class that inherits binders within the service, which provides public methods to expose the functionality of the service to the outside, and when other components and services are associated, the function of the service is invoked through the resulting IBinder instance.
Calling Unbindservice repeatedly throws an exception, so you need to make some judgments inside the program.
8) difference between started service and bind service: (Excerpt from: http://www.cnblogs.com/smyhvae/p/4070518.html )
Difference One: Life cycle
Services that pass the started mode are always running in the background and need to be stopped by the component itself or by external components to end the run
Bind-mode service, life cycle depends on bound components
Difference two: Parameter passing
The started service can pass parameters to the initiating service object, but cannot get the return value of the method in the service
The bind service can pass parameters to the initiating service object, or it can get the returned result by the bound business object
skills in practical development;
9) Service life cycle diagram:(Excerpt from:http://www.cnblogs.com/smyhvae/p/4070518.html )
10) Steps for IPC communication using Bindservice:
Note: The trigger cannot bind the service.
10.1) Inter-process communication on Android needs to be implemented through the Ibinder/binder framework, the Ibinder/binder interface is the basic interface of the Android remote object and describes the protocol that communicates with the remote object (that is, how to invoke the remote object, What are the features of the remote object).
Aidl can be used to enable a service to communicate across processes to components of multiple applications, which enables a service to be shared by multiple applications. The aidl is generally used to describe the interface that needs to be called by the client, which exists in the form of. aidl files, and is written in the same way as ordinary Java classes. Note: The Aidl file only acts as a template, and what really works is the. java file that ADT generates from the Aidl file.
This Java file generates an interface a that inherits from Android.os.IInterface as described in Aidl, and there is a static abstract class inside interface a Stub,stub class inherits the Android.os.Binder class and implements interface A.
There is also an internal static class inside the stub class Proxy,proxy also implements the interface A,proxy has a IBinder object inside, this proxy is to implement a real cross-process call to use.
10.2) Create a new class Aimpl, this class implements A.stub interface, this aimpl is the business object.
10.3) Create a new service, internally declare a variable mbinder of type Aimpl, and return the variable in the Onbind method.
11) If the configuration of the service is changed to: android:process= ": Remote", then the interface components and service components will run in different processes, it is not possible to directly bind the service, but only through the IPC call service.
In addition, if the remote service is called across processes, an implicit intent is required and an explicit intent cannot be used. And if a application is to invoke an interface in the B application, it is necessary to copy the Aidl file from the B application to the a application, including the package name.
12) through Messenger to achieve IPC communication, the bottom is still using aidl, specifically to build a messenger on the caller, and bind the service when the Messenger and binder binding (Messenger = new Messenger (binder); ); In the service of the callee, also create a new messenger, create a new handler, and let it bind to Messenger (when the message comes, let handler handle it), and return directly in the Onbind method Messenger.getbinder (); Return to Binder so that messenger and binder are bound so that the two messenger binds to the same binder and can communicate, which is done in a thread-safe and synchronous manner.
3, trigger component life cycle
The life cycle of a trigger component is the OnReceive method, which is immediately reclaimed by the system as soon as it executes.
When the OnReceive method executes, the trigger process is the foreground process.
The OnReceive method executes in the main thread and executes longer than 10 seconds into a unresponsive process with the risk of being forcibly reclaimed.
4, data Source component life cycle
When the data source component is constructed, the OnCreate method is called, and once it is constructed, it persists until the process is reclaimed.
5, the life cycle of the application environment object
As long as the process life cycle is constructed, the OnCreate method is called and the Onterminate method is called (and may not be called) when it is destroyed.
From for notes (Wiz)
Android Core Series--1, Component life cycle