System process that manages the running status of Activity and components--ActivityManagerService

AMS startup process

AMS is a system process provided by Android for managing the running status of Activity and other components. It is the most frequently used system service for writing apk. It is started SystemServerin and registered at the same time ServiceManager. Both it and WMS are "real-name" Binder Servers.

Like WMS, AMS is also hosted in systemServer. It will create a thread when the system starts to process customer requests in a loop. It is worth mentioning that AMS will register ServiceManagera variety of Binder Serverservices such as "activity", "meminfo", "cpuinfo", etc. - but only the first "activity" is the "main business" of AMS and is Activity ManagerServiceimplemented by; the functions of the remaining services It is provided by other classes.

Let’s first take a look at the startup process of AMS. As follows:

 /*frameworks/base/services/java/com/android/server/SystemServer.java*/
    public void run(){ 
        ...
        Slog.i(TAG, "Activity Manager");
        context = ActivityManagerService.main(factoryTest); //启动AMS
        ...
        ActivityManagerService.setSystemProcess(); //向ServiceManager注册AMS
    }

ActivityManagerService provides a static main function through which AMS can be easily started. Then you need to call setSystemProcess to register this important system service to ServiceManager. It can be seen that like WMS, it is a "real-name" Binder Server:

    /*frameworks/base/services/java/com/android/server/am/ActivityManagerService.java*/
    public static final Context main(int factoryTest) {
        AThread thr = new AThread(); //创建AMS线程
        thr.start(); //启动AMS线程
        synchronized (thr) {
            while (thr.mService == null) {
                /*注意,这段代码是运行在SystemServer所在线程中的。
                所以通过mService是否为空来判断AMS成功启动与否:如果是的话就可以返回SystemServer 继续执行,否则就一直等待。
                Android在处理“系统级进程”出错时的普遍态度是“既然系统都出错 了,任何补救都是无力回天的”,所以它的异常处理部分经常是空的
                 */
                try {
                    thr.wait();
                } catch (InterruptedException e) {
                }
            }
        }
        …
        m.mMainStack=new ActivityStack(m,context,true); //创建一个ActivityStack对象,这是AMS的核心,很多工作都是围绕它展开的
        ...
        return context;
    }

For the thread where SystemServer is located, it needs to wait until AThread (the above-mentioned variable thr) is successfully started before it can continue to execute. So after thr.start(), wait through thr.wait(). So, when do you wake up? The answer lies inside AThread:

 public void run() {
        …
        synchronized (this) {
            mService = m;
            mLooper = Looper.myLooper();
            notifyAll();
        }
    }

The notifyAll above will wake up all targets on the waiting queue where the thr object is located, which naturally includes the thread to which SystemServer belongs. The reason for this is that the subsequent operation of SystemServer will depend on AMS, so if you return rashly when AMS is not ready, it is likely to cause system downtime.
Registering AMS to ServiceManager is very simple. The only thing to note is that it not only registers its own Server, but a series of services related to process management. As follows:

    public static void setSystemProcess() {
        try {
            ActivityManagerService m = mSelf;
            ServiceManager.addService("activity", m, true);//AMS的主业ServiceManager.addService("meminfo", new
            MemBinder(m));//内存使用情况…//其他服务省略
        }
    }

core class

ActivityStack and ActivityTask, ActivityStack is the recorder and manager of Activity, and also provides the basis for AMS to manage the operation of the system. When creating an AMS thread, an ActivityStack is created; ActivityTask is a great tool in Android applications.

Android AMS source code reading analysis (1)

Android AMS source code analysis and reading (2)

Android AMS source code analysis and reading (3)

Guess you like

Origin blog.csdn.net/jxq1994/article/details/132631200