Android 新技术学习

Android之AMS介绍

2021-04-14  本文已影响0人  Lee_5566
image.png

目录

Android之AMS介绍

ActivityManagerService

AMS是Android中最核心的服务,主要负责系统中四大组件的启动、切换、调度及应用进程的管理和调度等工作,其职责与操作系统中的进程管理和调度模块相类似,因此它在Android中非常重要。

本次介绍是基于Android 8.0版本源码。

ActivityManagerService启动

ActivityManagerService的启动是在SystemServer.java中。

SystemServer会根据顺序依次启动各个服务:

        // Start services.
        try {
            traceBeginAndSlog("StartServices");
            startBootstrapServices();
            startCoreServices();
            startOtherServices();
            SystemServerInitThreadPool.shutdown();
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        } finally {
            traceEnd();
        }

我们跟踪下执行startBootstrapServices函数:

    private void startBootstrapServices() {
        ……

        // Activity manager runs the show.
        traceBeginAndSlog("StartActivityManager");
        mActivityManagerService = mSystemServiceManager.startService(
                ActivityManagerService.Lifecycle.class).getService();
        mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
        mActivityManagerService.setInstaller(installer);
        traceEnd();
        ……
    }

看一下ActivityManagerService.Lifecycle.class类,在ActivityManagerService 中:

    public static final class Lifecycle extends SystemService {
        private final ActivityManagerService mService;

        public Lifecycle(Context context) {
            super(context);
            mService = new ActivityManagerService(context);
        }

        @Override
        public void onStart() {
            mService.start();
        }

        public ActivityManagerService getService() {
            return mService;
        }
    }

以及SystemServiceManager.java的startService函数:

    public void startService(@NonNull final SystemService service) {
        // Register it.
        mServices.add(service);
        // Start it.
        long time = System.currentTimeMillis();
        try {
            service.onStart();
        } catch (RuntimeException ex) {
            throw new RuntimeException("Failed to start service " + service.getClass().getName()
                    + ": onStart threw an exception", ex);
        }
        warnIfTooLong(System.currentTimeMillis() - time, service, "onStart");
    }


其实最后就是执行了ActivityManagerService.Lifecycle.class的onStart函数,这样就把ActivityManagerService创建了。

image.png
上一篇下一篇

猜你喜欢

热点阅读