(v) AMS
If you stand on the four-component point of view, AMS is the server in binder.
AMS Full name is Activitymanagerservice, to see the literal meaning of management activity, but in fact, the four components are in it tube. It is estimated that the Android low-level developers first wrote Activitymanagerservice to manage the activity, and later wrote service, Receiver, CP found the code is similar, So they all use activitymanagerservice, but forgot to change the name--I guess, is purely gossip.
As a result of the plug-in, I remember 16 and Lody, Zhang Yong, Lin Guangliang together to eat supper, when I asked the two confused questions:
1) The installation process of the app, why not unzip the APK to the local, so that the picture can not be read every time from the APK package-this problem, we put in the PMS that section to say more detail.
2) Why hooks are always on the binder client side, that is, the four components side, not on the AMS side of the hook.
The second question is to be clearly explained here. Let's take an example of an Android Clipboard. As I said earlier, this is also a binder service.
It's really enough for AMS to be responsible for communicating with all four components of the app. If, in an app, the Clipboard function is tampered with at the AMS level, it will cause all the Clipboard functions of the Android system to be tampered with-this is the virus, and if so, the Android system is already dead. So the Android system does not allow us to do so.
We can only on the other side of the AMS, the client side, that is, the four components side of the tamper, so that even if we tamper with the Clipboard function, but also only affect the application of the tamper code, in other apps, the Clipboard function is normal.
About AMS we say so much, the following four components are described, the four components and the AMS of cross-process communication are mentioned repeatedly.
(vi) Activity 1th speaking
For developers who do apps, activity is the most and most complex of the four components, and I'm only talking about the activation and communication principles of activity. There are some related concepts, such as view, Looper, Intent, and Resource, which I will introduce later in another chapter.
Note: My analysis of the four components is based on Luo Shenyang's analysis of the android underlying book, I have deleted most of the code listed, only to keep those useful to the app developers of some code snippets and some key class names, and incorporated my understanding of the four components.
1) Start by figuring out how the app started.
Click on the icon of an app on your phone screen, assuming it's the Betta app, and the homepage (or guide page) of the app appears in front of us. This seemingly simple operation, behind the activity and AMs of the back and forth of the communication process.
The first thing to figure out, click on the app's icon shortcut icon on the phone screen, the mobile phone screen is an activity, and the activity of the app, the industry called launcher. Launcher is a mobile phone system manufacturers to provide, similar to Xiaomi Huawei Mobile phone, competition is who launcher gorgeous and humane.
Launcher This app, in fact, and we do all kinds of application class app is not different, we have used Huawei, Xiaomi and other mobile phones, preinstalled apps and we downloaded a variety of apps, are displayed on the launcher, each app is shown as an icon. Icon more can be paged, can be grouped, in addition, launcher will also initiate network requests, call weather data, displayed on the screen, so-called humanized interface.
Remember how we defined the default startup activity in the manifest file when we were developing an app? As shown below:
In launcher, the icon for each app provides the intent information needed to launch the app, as shown below (for example, the bucket fish package name):
Action:android.intent.action.MAIN
Category:android.intent.category.LAUNCHER
CMP: Bucket Fish Package name + Home activity name
This information is packagemanagerservice from the manifest file of the Betta apk package when the app is installed (or Android system boots).
So click on icon to launch the first page in the Betta app.
2) How easy is it to start the app?
The previous introduction is just one of the simplest overviews of app launches.
Looking closely, we will find that the launcher and Betta are two different apps, they are in different processes, and the communication between them is done by binder-this is when AMs comes out.
Still as an example of launching the Betta app, the overall process is:
- Launcher notify AMS to start the Betta app, and specify which page (i.e. the homepage) to start the betta.
- AMS Notice launcher, OK I know, not you what matter, at the same time, to start the first page to write down.
- Launcher the current page into the paused status, and then notify AMs, I slept, you can go to find the Betta app.
- AMS checks if the Betta app has been started. Yes, then evoke the Betta app. No, start a new process. AMS creates a Activitythread object in the new process, starting with the main function.
- After launching the Betta app, notify AMs that I am ready to start.
- Before AMs turns out the value stored in the second step, tell the Betta app which page to start.
- The Betta app launches the home page, creates the application, creates the context, and associates with the home page activity. Then invoke the OnCreate function of the first page activity.
The start-up process is now complete, divided into two parts, 1-3 steps, launcher and AMS communicating with each other, while the next few steps, the Betta app and AMS communicate with each other.
This will involve a bunch of classes coming in, listed below, in the next analysis, we will encounter:
- Instrumentation
- Activitythread
- H
- loadedapk
- Ams
- Activitymanagernative and Activitymanagerproxy
- Applicationthread and Applicationthreadproxy
1th Stage Launcher Notify AMS
This is my analysis of the activity in Lao Luo's book, My Own hand-painted UML diagram, a total of seven, that is, the activity started to experience the seven stages. It is recommended that the readers also hand-draw their own, so as to deepen understanding.
We see, click on the launcher on the Bucket Fish app icon shortcut icons, this will call Launcher startactivitysafely method, in fact, will call the activity StartActivity method, Intent with the key information you need to launch the Betta app as follows:
Action = "Android.intent.action.MAIN"
Category = "Android.intent.category.LAUNCHER"
CMP = "Com.douyu.activity.MainActivity"
The 3rd line of code is my guess, that is, the Betta app designates the activity in the Mainfest file as the first page. In this way, we finally understand why in the Mainfest, the first page to specify the action and category. During the installation of the app, this information is "recorded" in the Launcher Launcher shortcut icon. For the installation of the app, I'll explain it in more detail later in this article.
StartActivity This method, if we look at its implementation, we will find that it is tuned to go through a series of startactivity overloaded methods, and finally go to the Startactivityforresult method.
We know that Startactivityforresult needs two parameters, one is intent, the other is code, and here code is-1, which means that launcher doesn't care if the Betta app started successfully.
3rd Step: Startactivityforresult
The activity internally maintains a reference to the instrumentation, but anyone who has done the app unit test is familiar with the class, called the Dashboard.
In the implementation of the Startactivityforresult method, the Execstartactivity method of the instrumentation is called.
Seeing here, we find that there is a mmainthread variable, which is a variable of type Activitythread.
This guy is not very small.
Activitythread is the main thread, which is the UI thread, which is created when the app is launched and represents an app application.
What? Activitythread represents the app, wouldn't the application class be overhead? In fact, application may be important to our app developers, but it's really not that important in Android, he's a context. Doesn't activity have a context? Application is the context of the entire activitythread.
Activitythread is not that simple. It has a main function inside it.
We know that most programs have a main function, such as Java, C #, far from saying, the IPhone app used to Objective-c, also has the main function, then the main function of Android hide where? Just in Activitythread, as shown below, there's too much code, and I'm only intercepting part of it.
Someone else will ask? Who writes the program, who is going to provide the main function as an entry? But that's not the case with Android apps. The main function of the Android app is in Activitythread, and this class is the underlying class provided by the Android system, not provided by us.
So this is andoid interesting place. The entry for the Android app is defined as the default startup activity in Mainifest. This is determined by the communication mechanism between Android AMS and the four components.
Recently in the Wu-Yue version of the journey to the West, found that the west of the West, why use more than 10 years ah? Because the master and apprentice four of the road is nosy, so the delay for a long time. I also this article is so, often talk about on the topic, and so write down, do not know what time to write, so we all the way to the west, straight ahead, and then encountered Strange class, do not ignore it.
In return to the code, here are 2 important parameters to pass:
- A Binder object is obtained by Activitythread's Getapplicationthread method, which is of type Applicationthread, which represents the app process where launcher is located.
- Mtoken, this is also a binder object, it represents launcher this activity, here also through instrumentation to ams,ams a phone book, know who initiated the request to AMs.
These two parameters are foreshadowing, passed to AMs, after AMs want to turn to notify launcher, can pass these two parameters, find launcher.
The 4th step, the Execstartactivity method of instrumentation
Instrumentation is definitely the favorite of the Adnroid Test team because it can help us start the activity.
Back to our app launch process, in instrumentation's Execstartactivity method,
I understand that this is a pass-through, the activity of the data with the help of instrumentation, to activitymanagernative, not much interesting content, not much to say.
5th step: The Getdefault method of Amn
Activitymanagernative, referred to as AMN. This class is used over and over again.
Amn through the Getdefault method, an object named activity is obtained from the ServiceManager and then packaged as a Activitymanagerproxy object (AMP), and amp is the agent object of AMS.
Note 1:servicemanager is a container class.
Note 2:amn The Getdefault method returns a type of Iactivitymanager, not an amp. Iactivitymanager is an interface that implements IInterface, which defines the life cycle of all four components.
AMN and AMP are implemented Iactivitymanager interface, AMS inherit from Amn (Good chaos), then against the front aidl UML, it is not difficult to understand:
6th step, AMP's StartActivity method
See here, you will find the amp StartActivity method, and the Aidl proxy method, is identical, write data to another process, that is, AMS, and then wait for AMS to return the results.
At this point, the first phase of the work is done.
Follow-up process please participate in the next article.
Android Bottom-up knowledge for Android app developers (2)