AMS 启动过程

SystemServer.java

  /**
     * The main entry point from zygote.
     */
    public static void main(String[] args) {
        new SystemServer().run();
    }

创建SystemServer对象运行run()方法,点入run() 方法

//启动服务的关键代码
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() 可以看到启动ActivityManagerService的代码

//mActivityManagerService 通过反射启动AMS 服务
  // Activity manager runs the show.
        traceBeginAndSlog("StartActivityManager");
        mActivityManagerService = mSystemServiceManager.startService(
                ActivityManagerService.Lifecycle.class).getService();
        mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
        mActivityManagerService.setInstaller(installer);
        traceEnd();

SystemServiceManager 启动服务的代码,可以看到到就是就是通过Class.forName()


    public SystemService startService(String className) {
        final Class serviceClass;
        try {
            serviceClass = (Class)Class.forName(className);
        } catch (ClassNotFoundException ex) {
            Slog.i(TAG, "Starting " + className);
            throw new RuntimeException("Failed to create service " + className
                    + ": service class not found, usually indicates that the caller should "
                    + "have called PackageManager.hasSystemFeature() to check whether the "
                    + "feature is available on this device before trying to start the "
                    + "services that implement it", ex);
        }
        return startService(serviceClass);
    }

AMS 通过静态内部类启动

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

        public Lifecycle(Context context) {
            super(context);
            //创建AMS实例
            mService = new ActivityManagerService(context);
        }

        @Override
        public void onStart() {
        //启动AMS 服务对象
            mService.start();
        }

        public ActivityManagerService getService() {
        //获取实例对象
            return mService;
        }
    }

我们的AMS已经启动了,接着我们点击桌面上的图标以后就要启动我们的app了。
先上一张时序图

用户在Launcher程序里点击应用图标时,会通知ActivityManagerService启动应用的主Activity,ActivityManagerService发现这个应用还未启动,则会通知Zygote进程孵化出应用进程,然后在这个新孵化的应用进程里执行ActivityThread的main方法。

让我们先来看看ActivityThread 这个线程干了什么

   public static void main(String[] args) {

        ····
        Looper.prepareMainLooper();
        ActivityThread thread = new ActivityThread();
        thread.attach(false);
        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }

       ···
        Looper.loop();
        ···
    }

… 未完待续

你可能感兴趣的:(android)