1. Send the startActivity request to AMS
StartActivity calls AMS through Binder, takes AMS’s Binder object, and sends the request through mRemote’s Transact function. Transact encapsulates all request parameters into a Parcel called Data, which is then written to the Binder driver, which forwards it to AMS. AMS receives a Transact callback that extracts all request parameters from data and processes the request based on the request code, as shown below
2. How does AMS handle startActivity
The ProcessRecord process exists and the application has registered its ApplicationThread with AMS. If yes, call realStartActivityLocked to start the Activity. If not, start the process and call startProcessLocked. After the process is started, start the ActivityThere is an entryPoint in startProcessLocked, which is the Java class that applies the entry function after it is started. Process.start sends Zygote a request to start the Process, which then knows which class’s entry function to execute. Zygote at boot after the Process Pid will return Process, in the Process. ProcessStartResult, to add a mPidsSelfLocked Pid as the key, ProcessRecord is the value of the record. If the application process does not report to AMS for a certain period of time after restarting, it will time out, and the timeout period is 10 seconds. After the timeout, AMS will clear the information related to the application, such as some application components to be started.StartViaZygote does two things: first, open the socket to Zygote, second, send the start process parameter through the socket, and then read the return result. In the runOnce function, Zygote first reads the list of parameters sent by AMS, and then creates the child process. The parent process returns the Pid of the child process to AMS through the Socket. The child process does some general initialization via handleChildProc, such as launching the Binder mechanism and calling the application’s entry function, ActivityThread’s main function
Create an ActivityThread object and register it with AMS via attach. AMS sends a request to Zygote to start the process and waits for the application to report to Zygote. If the application does not report to Zygote within 10 seconds, it will be cleared. Attach is implemented as a cross-process call that takes the AMS Binder object and calls attachApplication. The attach parameter is the Binder object on the application side, so that AMS can call the application. Look up ProcessRecord in the map based on the Pid, and then call bindApplication with the Binder object registered with AMS on the Application side to make the Application do some initialization, such as creating the Application, and then handle the Application components that are suspended because the Application process is not started.Get the mFocusedStack of the admin application, call topRunningActivityLocked, find the nearest task from the stack, pull the Activity at the top of the task stack, and start the Activity.App is ProcessRecord corresponding to the application process, Thread corresponding to the binder object, which is registered with AMS when the application starts. Now AMS initiates binder calls to the application through this binder object, which is called scheduleLanchActivity. When the application loads the Activity, the application encapsulates a message to drop into the main thread.LoadedApk stores the Apk information. After creating an Activity, you will use the classLoader to load the Activity class in the Apk.After you generate the appContext, you attach it to the Activity, you give the Activity a context, and you initialize it inside of attach.The onResume life cycle is as followsSo in generalIn addition to AMS and Zygote are Socket communication, the rest are all Binder communication