Basic architecture of Android ActivityManagerService

Source: Internet
Author: User

It may take some time to learn about AmS. In summary, you can also sort out your own ideas. I am a small soldier. I don't understand it in some places. Can we discuss and communicate with each other to make progress ~~~

AmS is one of the core modules of the Android upper-layer system. It mainly manages the lifecycle of application processes and activities, services, Broadcast, and providers of processes.

From the perspective of system running, AmS can be divided into Client and Service: the Client runs in various app processes, and the app process implements specific activities and services, tell the system that I have activities, services, and so on, and call the system interface to display them. The system-level ActivityManagerService running on the SystemServer process is the specific implementation of the system-level ActivityManagerService, which responds to the system call request from the Client, and manage the lifecycle of each app process on the Client.

Figure 1 AmS basic class diagram

As shown in the preceding basic AmS class diagram, the Application, Service, and Activity we normally use on the Client side are subclasses of Context. Context is understood as environment and Context, that is, it can tell the system the current status of the Activity and Service I am running, display those views, and do those jobs. Context is an abstract class. The defined methods are implemented in ContextImpl. ContextWrapper is used as the decoration class of Context. The member variable mBase points to ContextImpl. Of course, ContextImpl is not required here, any interface defined in the Context can be instantiated.

In addition, we noticed that many Android interfaces must be called to pass in the Context object. Normally, we directly pass in the Activity object as a parameter. If the method internally defines global variables or static variables, mContext reference is used for a long time. This will cause the Activity object to be unable to be recycled by JVM. Such reference may cause OOM. We noticed that the Application is also inherited from the Context, so the Application is also the Context. We know that the Application is created when an Application is started, so our app has always had a global Context object-Application object, therefore, we can pass the Application object on some known method calls that use the Context object reference globally to avoid the OOM mentioned above.

The IActivityManager interface defines the interface for an app to access AmS. It mainly refers to the application requesting AmS to complete some operations, such as starting or finishing an Activity, starting or stopping a Service. ActivityManagerService implements the interface defined in IActivityManager. This class can be said to be the core of AmS. All the specific work of AmS is basically in this class or controlled through this class, the ActivityManagerService instance is initialized when the SystemServer process is started. The IApplicationThread interface defines the interfaces that AmS can access the app. AmS controls the app process and completes the app response through these interfaces. ApplicationThread is the specific implementation of the IApplicationThread interface, the ApplicationThread instance is initialized when the ActivityThread object is created when the app process starts. The member variable mAppThread of ActivityThread is the ApplicationThread object. In addition, to implement cross-process calls, ActivityManagerProxy and ApplicationThreadProxy implement the IActivityManager and IApplicationThread interfaces respectively, which serve as their respective proxies for the client and server.

AmS is also a system service. ActivityManager defines some excuses for apps to use. The interfaces used to access AmS in ActivityManager are implemented through ApplicationThreadProxy.

If it is the main data structure class diagram in AmS, I believe everyone will know what these classes are doing after reading the code. Here is a summary.

Figure 2 main data structure charts in AmS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.