ActivityManagerService启动流程简析

    ActivityManagerService(AMS)是Android中核心的服务,主要负责系统中四大组件的启动、切换以及应用进程的管理和调度等工作。下面主要是查看ActivityManagerService的启动流程,AMS由SystemServer.java启动,zygote进程会调用SyetemServer.java中的run方法开启相关服务。

一、SystemServer.java

frameworks/base/services/java/com/android/server/SystemServer.java

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

1、run方法
private void run() { 
   try {
        ...
         // Initialize the system context.
     createSystemContext();

     // 创建,启动和管理SystemService的生命周期
      mSystemServiceManager = new SystemServiceManager(mSystemContext);
     // 添加到本地服务的全局注册表中。
     LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
    } finally {
     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
    }

   // Start services.
  try {
      Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices");
     startBootstrapServices(); //开启引导服务
     startCoreServices(); //开启核心服务
     startOtherServices(); //开启其他服务
    } catch (Throwable ex) {
     Slog.e("System", "******************************************");
     Slog.e("System", "************ Failure starting system services", ex);
     throw ex;
    } finally {
     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
    }
...
}

ActivityManagerService属于引导服务,下面查看下startBootstrapServices()方法。

2、startBootstrapServices()

    该方法中主要开启了ActivityManagerService,PowerManagerService,LightsService,DisplayManagerService等服务, ActivityManagerService不像其他服务直接继承SystemService,ActivityManagerService中定义了一个内部类Lifecycle,该类继承SystemService,SystemServiceManager通过Lifecycle开启ActivityManagerService。相关代码如下:

private void startBootstrapServices() {
     Installer installer = mSystemServiceManager.startService(Installer.class);
      ...
     mActivityManagerService = mSystemServiceManager.startService(
                ActivityManagerService.Lifecycle.class).getService();
    mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
    mActivityManagerService.setInstaller(installer);

    ...

 }
3、SystemServiceManager.startService(Class serviceClass)

frameworks/base/services/core/java/com/android/server/SystemServiceManager.java

通过反射机制获service取对象,并调用对应service的start方法。

public  T startService(Class serviceClass) {
        try {
            final String name = serviceClass.getName();
            ....
            final T service;
            try {
                Constructor constructor = serviceClass.getConstructor(Context.class);
                service = constructor.newInstance(mContext);
            } catch (InstantiationException ex) {
               ...
            }

            // Register it.
            mServices.add(service);

            // Start it.
            try {
                service.onStart();
            } catch (RuntimeException ex) {
                ...
           }
    }

4、Lifecycle.start()

    SystemServiceManager通过startService获取ActivityManagerService的内部类Lifecycle对象,并通过Lifecycle开启和获取ActivityManagerService服务对象,这样ActivityManagerService就被创建和开启了。

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

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

        public Lifecycle(Context context) {
            super(context);

            /// M: Add BootEvent for profiling
            BootEvent.setEnabled(true);

            mService = new ActivityManagerService(context);
        }

        @Override
        public void onStart() {
            mService.start(); //开启ActivityManagerService
        }

        public ActivityManagerService getService() {
            return mService;
        }
    }




你可能感兴趣的:(Framework)