The main method of the ActivityThread is the main method of the Android application, as shown below:

Then you see the attach method that calls ActivityThread as follows:

What type is an mAppThread object? As follows:

Back to the MGR. AttachApplication (mAppThread); IActivityManager is an interface. Its inheritance tree structure is shown below. I mentioned earlier that MGR is the local Proxy of ActivityManager, so the actual type of MGR is Proxy. Therefore, attachApplication(mAppThread) is invoked in the Proxy as follows:

After the mgr.attachApplication(mAppThread) call is completed, the “double” communication channel between Android and ActivityManager is established. For your understanding, I draw a picture as follows:

The ActivityManager service can control the main thread of an Android application once it has the ApplicationThread local agent. For example, the Android application ApplicationThread.Stub scheduleLaunchAc is triggered by calling the scheduleLaunchActivity of the ApplicationThread local agent That’s called tivity, and that’s all very well understood if you understand the Binder mechanism. If you don’t, check out my previous article about the Binder mechanism: https://www.jianshu.com/p/5de1718454f8 ApplicationThread. Stub scheduleLaunchActivity method is as follows:

mH.sendMessage(msg); The activityThread. H handleMessage will be called as follows:

1. Create an ApplicationThread object on the main thread of ActivityThread. In other words, other processes can communicate with the main thread (ApplicationThread object) of my ActivityThread through the object’s local proxy

2. Obtain the local proxy object of ActivityManager

3. “send” the generated ApplicationThread object to ActivityManager via ActivityManager’s local proxy object. The system service ActivityManager then “generates” a local proxy for the ApplicationThread object, and a “duplex” communication channel is established between this main thread and the system service ActivityManager

4. When you want to start an Activity, the system service ActivityManager calls the scheduleLaunchActivity method of the ApplicationThread object’s local agent. The main thread’s scheduleLaunchActivity is called accordingly, starting the callback to the Activity lifecycle function.