欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

AndroidActivityManagerService启动流程详解,

来源: 开发者 投稿于  被查看 47976 次 评论:110

AndroidActivityManagerService启动流程详解,


目录
  • 概述
  • AMS的启动流程
  • 启动流程图

概述

AMS是系统的引导服务,应用进程的启动、切换和调度、四大组件的启动和管理都需要AMS的支持。从这里可以看出AMS的功能会十分的繁多,当然它并不是一个类承担这个重责,它有一些关联类。

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; 
  }
​

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

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();
       }

在创建完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");
            }
 }           

启动流程图

到此这篇关于Android ActivityManagerService启动流程详解的文章就介绍到这了,更多相关Android ActivityManagerService内容请搜索3672js教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持3672js教程!

您可能感兴趣的文章:
  • Android AMS启动详解

用户评论