【安卓开机启动】AMS启动流程

【安卓开机启动】 AMS启动

本文参考https://blog.csdn.net/renshuguo123723/article/details/85332146

AMS,即ActivityManagerService,是安卓java framework的一个服务,运行在system_server进程。此服务十分重要,因为它管理着安卓的四大组件,是安卓APP开发者最常接触到的一个服务。

AMS的启动流程
1:SystemServer#main -> 2:SystemServer#run -> 3:SystemServiceManager#startBootstrapServices
1:首先SystemServer进程运行main函数, main函数内部只调用了一个自己的run()方法.

 public static void main(String[] args) {
        new SystemServer().run();
    }

2:SystemServer的run()方法注释非常多,可以自己看一下.中文注释没有使用//了, 不然颜色比较难以看清

 private void run() {
       <--开始!-->
            // Prepare the main looper thread (this thread).
            <--准备主线程(该线程)-->
            android.os.Process.setThreadPriority(
                android.os.Process.THREAD_PRIORITY_FOREGROUND);
            android.os.Process.setCanSelfBackground(false);
            Looper.prepareMainLooper();
            Looper.getMainLooper().setSlowLogThresholdMs(
                    SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);

            <--初始化native服务-->
            System.loadLibrary("android_servers");

            <--初始化系统上下文-->
            createSystemContext();

            <--创建system service manager!!!-->    
            mSystemServiceManager = new SystemServiceManager(mSystemContext);
            mSystemServiceManager.setStartInfo(mRuntimeRestart,
                    mRuntimeStartElapsedTime, mRuntimeStartUptime);
            LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
            // Prepare the thread pool for init tasks that can be parallelized
            SystemServerInitThreadPool.get();
 

       <--打开系统服务-->
            <--启动引导服务-->
            <--用SystemServiceManager启动了ActivityManagerService、PowerManagerService、 PackageManagerService等服务-->
            startBootstrapServices();

            <--核心服务-->
            <--启动BatteryService、UsageStatsService和WebViewUpdateService-->
            startCoreServices();

            <--启动其他服务-->
            <--启动了WMS,CameraService、AlarmManagerService、VrManagerService等服务-->
            startOtherServices();

      <--Loop forever-->
        Looper.loop();
        throw new RuntimeException("Main thread loop unexpectedly exited");
    }

3:SystemServer的startBootstrapServices()方法

SystemServer.java
//启动AMS
 private void startBootstrapServices() {
 		...
		<--调用SystemServiceManager的startSErvice()方法, 传入Lifecycle.class字节码文件的参数, 
		通过反射实例化Lifecycle对象,并启动AMS(通过这个参数"ActivityManagerService.Lifecycle.class"可以看出
		Lifecycle是AMS的一个内部类)-->
        mActivityManagerService = mSystemServiceManager.startService(
                ActivityManagerService.Lifecycle.class).getService();

        mActivityManagerService.setSystemServiceManager(mSystemServiceManager);

        mActivityManagerService.setInstaller(installer);
 }

SystemServiceManager.java
 public SystemService startService(String className) {
       	<--使用反射获取.class文件-->
        Class serviceClass = Class.forName(className);
        return this.startService(serviceClass);
    }
SystemServiceManager.java
 public <T extends SystemService> T startService(Class<T> serviceClass) {
        String name = serviceClass.getName();
        SystemService service;
        Constructor<T> constructor = serviceClass.getConstructor(Context.class);
        
        <--通过反射,调用constructor的newInstance方法来创建Lifecycle类型的service对象-->
        service = (SystemService)constructor.newInstance(this.mContext);
        
        <--把该服务加入到系统服务集合当中, 该系统服务集合就是SystemServiceManager类的list类型的成员变量-->
    	this.mServices.add(service);
    	
    	<--调用反射类的onStart()方法开启AMS服务(实际上Lifecycle内部类虽然是静态的, 
    	但是显示的拥有一个AMS的对象, 该方法就是利用这个AMS对象调用AMS的start()方法)-->
		service.onStart();
		<--返回该服务-->
        return service; 
  }

2021年7月4日补全

19年的时候初入安卓系统开发的岗位,对整个系统的认识比较少。
现在看来AMS的启动过程相对来说是比较简单的。
在系统开机启动之后,system_server会执行三大服务启动

  1. startBootstrapServices() 启动引导服务,在这里实际上已经new了AMS,在new方法里,已经初始化了AMS的大部分重要的属性。AMS的Looper和各种handler也是在这里准备好的。
private void startBootstrapServices() {
   	...
       ActivityTaskManagerService atm = mSystemServiceManager.startService(
               ActivityTaskManagerService.Lifecycle.class).getService();
       		mActivityManagerService = ActivityManagerService.Lifecycle.startService(
               mSystemServiceManager, atm);
       		mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
       		mActivityManagerService.setInstaller(installer);
       ...
}

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

       public Lifecycle(Context var1) {
           super(var1);
           this.mService = new ActivityManagerService(var1, sAtm);
       }

       public static ActivityManagerService startService(SystemServiceManager var0, ActivityTaskManagerService var1) {
           sAtm = var1;
           return ((ActivityManagerService.Lifecycle)var0.startService(ActivityManagerService.Lifecycle.class)).getService();
       }
  1. 在创建完AMS之后,system_server的run方法会执行到startOtherServices(),在启动“其他服务”完毕之后,会调入到AMS的systemReady()方法,在这里会启动launcher。
    可以说,在这个方法执行完毕之后,系统就已经启动完成了。如果我们先要在系统启动的过程中在java framework中加入自己的代码,可以在systemReady()这个方法中,完成。(比如注册自己的广播接受器)
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
            if (bootingSystemUser) {
                mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");
            }
 }           

最后在强调一下AMS的功能吧,它主要是用来管理四大组件的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值