The first two days of the project is not how busy, take a sneak, carrying the eldest brother looked at the next Activitymanagerservice (Android 2.3) some east, shallow understanding, only when the introduction ...
Go to the Chase:
The main classes and explanations involved
1. Ams---activitymanagerservice.java, Android system services, activity management services, for managing activity activities, controlling activity life cycle, distributing message events, low memory management and so on. Implements the IBinder interface, which can be used for interprocess communication.
2. Applicationthread.java, implements the interface of the IBinder interface, the client and the server (AMS) communication throughout the framework. It is also the inner class of the class Activitythread. This effectively binds AMs and activitythread together. Proxy mode? A bit like ...
3. Activitythread.java, as described in 2nd, the client that Applicationthread binds to is activitythread. Activitythread This class is very important in the activity client.
A. It is an entrance to the application, as is known to the Java program's entry when the main () method, similarly, when AMS pulls up a new process, while starting a main thread, The main thread is executed from the Activitythread.main method, which initializes some objects and then enters the message waiting queue itself, which is looper.loop (); Once in the loop () method, the thread enters a dead loop, never quits, waits for someone to give him a message, and then executes the message. This is also the principle of the EDT (event-driven model).
B. It is the management class of the activity client, which determines when to call OnCreate () and when to call the Onresume () method. When the activity initiates a request, such as startactivity (), or finish (), it handles the request and calls the other person to do the work.
4. Instrumentation.java, this class is a person who actually does things in activity management, in addition to testing Android. For example, StartActivity (), in some cases, calls this class and then calls to AMs. Of course, there is a time to visit AMS through Applicationthread.
Question: Some students will think, since Applicationthread can access AMS, then our application can call the Applicationthread class to access AMS, directly start the activity?
The answer is no. Because Applicationthread is a private inner class of Activitythead, the outside world cannot access it. And Activitythread is hide, so our application cannot access the Activitythread class through the SDK. Similarly, the interface that gets the AMS reference is the Activitymanagernative class, and this class is also hide. Therefore, the application does not directly operate AMS.
5. Activity.java, this class is again familiar, one of the most important components of Android, in fact, from the Java point of view, it is not special, it is a Java object, it will be created, but also by the garbage collection mechanism is destroyed, but it is managed by AMS, so it has its life cycle. So it seems more important.
6. Activityresult.java, Activity Management Server, activity's record cache. In other words, the client initiates an activity and the AMS caches it, and the type is Activityresult. We might wonder, why not just cache the value of the activity type, but define a new Activityresult type?
Because the activity class does not implement the IBinder interface, it cannot be used for communication between processes. This cache value is used for AMS and client communication. Therefore, the IBinder interface must be implemented. And when the activity is designed, it is not intended to be used for process communication, and to see its member variables, there are many large objects, such as list, map and so on. These objects are not intended to be transferred across processes. Because the cross-process invocation in Java is serialization and deserialization, those large objects consume a lot of time and resources when serializing and deserializing. In addition, according to the design rules, the role of a class as single as possible, do not appear that kind of universal class. So that's one of the reasons.
What is AMS?
The full name is Activitymanagerservice, as the name implies, is the server side of the activity management mechanism. belongs to a system service, the location of the systemprocess process.
AMS can be viewed from multiple perspectives;
1. From the Java point of view, AMS is a Java object that implements the IBinder interface, so it is an interface for interprocess communication.
This object is initialized in the Systemserver.java run () method.
SLOG.I (TAG, "Activity Manager");
Context = Activitymanagerservice.main (factorytest);
This is a very common Java static Factory code, where we create our AMS object.
Initialize method
public static final Context main (int factorytest) {
Athread thr = new Athread ();
Thr.start (); Start a thread to initialize the AMS object
Synchronized (THR) {
while (Thr.mservice = = null) {
try {
Thr.wait (); Wait for Athread to initialize AMS complete
} catch (Interruptedexception e) {
}
}
}
...
.....
}
Athread's Run () method
public void Run () {
Looper.prepare ();
Android.os.Process.setThreadPriority (
Android.os.Process.THREAD_PRIORITY_FOREGROUND);
Android.os.Process.setCanSelfBackground (FALSE);
Activitymanagerservice m = new Activitymanagerservice (); Initializing the AMS object
Synchronized (this) {
Mservice = m;
Notifyall (); The initialization completes, notify the Wait () method of the Main method, tells that it is finished, and can go down.
}
......
Looper.loop (); Enter the message loop wait state yourself
}
Thinking:
Here is another thread athread to create the AMS object, and then use the wait/notify mechanism to implement synchronization between threads.
A. Creating an object completely eliminates the need to open another thread to handle it. Have the feeling of superfluous
B. Athread after the AMS object is created, it enters the message loop state instead of exiting directly. But looking at the code does not find a place to send messages to the thread's handler, which means that the state of the athread thread will be blocked forever. Don't understand what that means.
Thought result:
It's Athread's Run method inside New Activitymanagerservice (), and Activitymanagerservice has a variable mhandler inside it, In this case, Mhandler bound thread is athread this thread. By sending a message to Mhandler, Athread continuously gets the message from the message queue and processes the message. Therefore, the above a, a, problem has been solved.
2. AMS is a service
Activitymanagerservice from the name, it is a service that manages activity and is a system service like package management Services, battery management, and vibration management services.
3. AMS is a binder
AMS implements the IBinder interface, so it is a binder, which means that it can be used not only for interprocess communication, but also for a thread. Because a binder is a thread.
How does that make sense? It can be understood that if we launch a Hello World Android app that doesn't have another thread in it, then I think we're starting at least 4 threads in this process.
A. Main thread, which is the main thread of the program and the one that we use the most when we write our code everyday. For example, we rewrite the activity's OnCreate () method, which is typically the main thread to execute code, also called the UI thread. This is because Android components (including TextView, Button, and so on) are non-thread-safe, so only the main thread is allowed to operate.
B. GC Thread. As we all know, Java has a garbage collection mechanism, and each Java program (a Java Virtual machine) has a garbage collection thread that specifically reclaims objects that are no longer in use.
C. Binder1 is our applicationthread, this class implements the IBinder interface for interprocess communication. Specifically, it is our application and the AMS Communication tool.
D. Binder2 is our VIEWROOT.W object, it is also the implementation of the IBinder interface, is used for our application and WMS communication tools.
PS:WMS is the concept of windowmanagerservier, similar to AMS, but it is a system service for the management window.
4. AMS is a separate thread
A. First, system services cannot run in the main thread of the systemprocess process. Because so many system services are running in the main thread, there is bound to be a blockage. For example, this thread is managing the activity, and then the phone calls vibrate, then no one is handling the vibration management. So the General Service is running in a separate thread (some service exceptions)
B. AMS implements the IBinder interface, so it is a separate thread, which is the thread that handles the management of the activity by AMS.
Question: If AMS has only one thread to perform activity management, there will be no blocking. For example, two clients initiate a request at the same time.
My own opinion is not supposed to. Because the mobile operating system is a single-interface operating system, that is, at the same time, only one activity can be displayed in front of the user (tabactivity appears to show a number of activity at the same time, but Android did a special processing, not discussed), Unlike Windows, which is a multi-tasking, multi-window operating system. Single-window words, then send the OnPause (), Onresume () such as the request, at the same moment, should only be initiated by one person.
AMS Management Activity Process
1. Take Activity.startactivityforresult () as an example, the most common we see is to call the Activity.startactivity () method. However, the internal implementation of startactivity () also calls the Startactivityforresult () method, but Requestcode=-1
。 Activity.startactivity ();
Here activity start an activity is asynchronous. In other words, the activity does not have to wait for the AMS result to return, directly to the following execution. When the next code is finished, it goes into the message loop waiting for the queue and continues to wait for someone to give him the message. Otherwise it will always be blocked. However, AMS will soon send it a message suspending the current activity. is the following invocation process.
Instrumentation. Execstartactivity ();
Ams.startactivity ();
Activitystack. Startactivitymaywait ();
When calling Activitythread's Pause method, it also sends a pause message to itself, delaying 500ms execution, and if the message is not removed after 500ms, it will say that there is a pause exception.
The point here is that after AMS sends a pause message to Activitythread, it goes straight down. So this is an asynchronous call, non-blocking. After sending the message, wait for Activitythread to return the paused message to itself.
Activitythread. Schedulepauseactivity ();
Pause Complete Call Activitymanagernative.getdefault (). activitypaused (token, state);
Tell AMs that it has paused.
The above 500ms message is removed from the ams.activitypaused () method. Then call the new activity's Onresume method.
2. The Startactivityforresult () method initiates an activity that, after the end of the target acitivty, calls to the Onactivityresult () method.
This process is also similar to the Activity.finish () method is the entry, AMS after receiving the finishactivity (), will be called to the Activity.onactivityresult () method.
The activity and Activitymanagerservice here are through binder to complete interprocess communication.
Four-graph diagram
Application Process System Process
Activity1
Activity2 Activitythread Applicationthread Ams
Activity3
Originally wanted to upload a map, but the company upload is not convenient, can only be described in words. Sorry.
This means that Activitythread communicates with AMS by appliactionthread this binder.
Here is only a preliminary analysis of the next AMS, in fact, it has a lot of functions, such as process, task management, low memory management and so on.
There is an oom module in Android and this module will work when the system memory is not enough. In fact, AMS does not operate the Oom module directly, but the Oom module has a priority when killing the process to release resources Oh Oom_adj ( -16~15), the lower the value, the more important the description. AMS also manages individual applications by setting the value of Oom_adj to killer.
In fact, we analyze these things is not to modify AMS, just to know it, know its why. So that we can write more robust applications. At the same time, our code capability is also an improvement.
Initial analysis, welcome all kinds of board bricks, rotten eggs.
An analysis of Android AMS