AMS 的启动过程

从前面学习知道,系统启动时,会启动SystemServer进程,而SystemServer进程又会启动各种系统服务,包括AMS,那么这个过程是怎么样的,现在就从SystemServer的main()进入看看。

SystemServer.main()
 public static void main(String[] args) {
    new SystemServer().run();//调用了自身的run()方法
 }
    
SystemServer.run()
private void run() {
  ...
  try{
    ...
    //创建消息Looper
    Looper.prepareMainLooper();

   // 加载动态库libandroid_servers.so
   System.loadLibrary("android_servers");
   performPendingShutdown();

    //创建ActivityThread以及系统的Context
    createSystemContext();
    
    //创建SystemServiceManager,负责创建、启动、管理系统服务
    mSystemServiceManager = new SystemServiceManager(mSystemContext);
    mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
    LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
    // Prepare the thread pool for init tasks that can be parallelized
    SystemServerInitThreadPool.get();
    
    ...
    
    try {
            traceBeginAndSlog("StartServices");
            //启动引导服务,例如AMS、PMS,那么就去看一下如何启动则个AMS服务。
            startBootstrapServices();
            //启动核心服务例如BatteryService、WebViewUpdateService
            startCoreServices();
            //启动其他服务
            startOtherServices();
            SystemServerInitThreadPool.shutdown();
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        } finally {
            traceEnd();
        }

SystemServer.startBootstrapServices()
private void startBootstrapServices() {
  ...
  traceBeginAndSlog("StartActivityManager");
  
  //1.首先调用startService()去创建AMS,如何创建?
  mActivityManagerService = mSystemServiceManager.startService(
  ActivityManagerService.Lifecycle.class).getService();
  //2.给AMS设置相关属性
  mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
  mActivityManagerService.setInstaller(installer);
 traceEnd();
 ...
}      
ActivityManagerService.Lifecycle.class

上面startService()传入了ActivityManagerService.Lifecycle.class作为参数,那么它是什么?

//Lifecycle是ActivityManagerService的一个内部类,继承自SystemService
public static final class Lifecycle extends SystemService {
        private final ActivityManagerService mService;

        public Lifecycle(Context context) {
            super(context);
            //创建ActivityManagerService对象
            mService = new ActivityManagerService(context);
        }

        @Override
        public void onStart() {
            mService.start();
        }
		 //提供获取ActivityManagerService方法
        public ActivityManagerService getService() {
            return mService;
        }
    }
SystemServerManager.startService(Class serviceClass)
public <T extends SystemService> T startService(Class<T> serviceClass) {
   try {
      //1.获取class的名字,这里就是ActivityManagerService.Lifecycle
      final String name = serviceClass.getName();
            
      Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);

       //2.如果不是继承自SysteService则会抛出异常。
        if (!SystemService.class.isAssignableFrom(serviceClass)) {
         throw new RuntimeException("Failed to create " + name + ": service must extend " + SystemService.class.getName());
        }
        
         final T service;
            try {
            	   //3.通过反射去创建ActivityManagerService.Lifecycle的实例, ActivityManagerService.Lifecycle创建同时,内部会创建AMS实例
                Constructor<T> constructor = serviceClass.getConstructor(Context.class);
                service = constructor.newInstance(mContext);
            } 
            ...
            //4.然后调用startService(service);
            startService(service);
            return service;
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
        }
    }
SystemServerManager.startService(SystemService service)
 public void startService(@NonNull final SystemService service) {
        // 首先将service添加到list中
        mServices.add(service);
        // Start it.
        long time = System.currentTimeMillis();
        try {
        		//2.调用service的onStart()方法启动,这里的service指的是 ActivityManagerService.Lifecycle,内部调用的是AMS的onStart()方法
            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");
    }

至此,AMS的创建和启动就都已经完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值