Android Application startup process

  Reprinted: http://www.51testing.com/html/34/n-3725434.html  
​ After you have a basic understanding of the whole, go to the details

  When it comes  to the startup process of Android Application , many articles are a bunch of calling relationships of various source code classes and methods. Even if such articles are read a hundred times, they are only in the fog. The source code has to be seen, but it is best not to get caught up in the details of the source code all of a sudden. Here is a summary based on the previous ones.

  Before talking about the startup process of the application, you must first understand the startup process of the Android system, because the startup of the Application is inseparable from the processing of the system.

  Android system startup process

  1. BootLoader starts the kernel and init process;

  2. The init process has split into a daemon process, such as Android Debug Damon, USB Damon, these daemon processes will handle some hardware-related interfaces;

  3. The init process starts a Zygote process

  The Zygote process initializes the first VM and preloads the Framework and some general resources.

  The zygote process will open a Socket interface to listen for requests. Once requested, Zygote spawns a new VM based on its own preloaded VM and creates a new process.

  4. After starting Zygote, the init process will start the Runtime process. Zygote will hatch a super management process - System Server. System Server will start all system core services, such as Activity Manager Service and hardware-related services.

  5. At this time, it is ready to start its first App process - the Home process.

  The Android system has been started, some core services have also been started, and then the Launcher application is started, so when will the application process be started?

  When is the App process created?

  Answers are created when they are needed.

  If an application (mostly Launcher) calls a page in the App, if the target process does not exist, a new process will be created and started.

  Application startup process

  Before talking about the Application startup process, let's look at a previous flow chart:

  App startup process

  When analyzing the process, you can look at the above flow chart at the same time.

  Click on Desktop Icon

  Then call the StartActivity(Intent intent) method;

  This method will eventually call ActivityManagerService through Binder IPC, which is referred to as AMS here.

  AMS does the following:

  Finally, the pointer information of this Intent object will be collected through the resolveIntent() method of PackageManager (there will be many classes and method calls in the middle).

  Verify that the user has sufficient permissions to call the target Activity through the grantUriPermissionLocked() method;

  Query whether ProcessRecord exists

  If it does not exist, AMS will create a new process to instantiate the target Activity.

  Next, let's talk about the creation process of the App process.

  App process creation

  Call the startProcessLocked() method to create a new process

  Pass the parameters to the Zygote process through the Socket channel mentioned above, the Zygote process incubates itself, and calls the ZygoteInit.main() method to instantiate the ActivityThread object, and finally returns the pid of the new process.

  ActivityThread calls Looper.prepare() and Looper.loop() methods in turn to start the message loop.

  At this time, the process has been created, but how to connect with the Application of the application itself?

  Application binding

  Call the bindApplication() method in ActivityThread to send a BIND_APPLICATION message to the message queue.

  Process the previous binding message through the handleApplication() method;

  Call the makeApplication() method to load the Application class into memory.

  The general process is as follows. If you need to do some special processing, you still need to go deep into the source code, find the points you can handle, and perform some customized processing.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325261233&siteId=291194637