当前位置:   article > 正文

Android 8.0 ActivityManagerService 启动流程_activitymanager: load

activitymanager: load

ActivityManagerService(以下简称为 AMS)是 Android 中最核心的系统服务之一,我认为 AMS 最重要的功能有两个:

  1. 对应用程序进程的管理:应用程序进程的创建、销毁和优先级的调整
  2. 对应用程序进程中的四大组件进行管理:最常见的 Activity、Service 等四大组件的生命周期方法都是通过 AMS 间接地调度执行的

这篇文章对 Android 8.0 系统中的 AMS 启动流程加以分析。

一. 整体结构

首先,我们来看一下 AMS 中管理应用程序进程和应用程序进程中四大组件的相关类和他们的关系图

 

 

图片来源:Android7.1 ActivityManagerService概述

ActivityThread 是应用程序进程的入口,负责对应用程序进程中主线程的管理,被 AMS 调度从而直接调用四大组件的生命周期方法

在 AMS 中并不能直接管理四大组件,四大组件在 AMS 中都有一个对应的类,AMS 中管理的是 ActivityRecord 等对象,再通过 Binder 通信向 ApplicationThread 发送消息,ApplicationThread 再通过 Handler 将消息发送到主线程中,最后就会去调用四大组件的生命周期方法

应用程序进程中的四大组件 AMS 中的四大组件
Activity ActivityRecord
Service ServiceRecord
Broadcast BroadcastRecord
ContentProvider ContentProviderRecord

接下来,我们分析下 AMS 的启动流程

二. SystemServer 的启动

和之前分析 WMS 的启动一样,从 SystemServer 中开始分析。

2.1 SystemServer 初始化

  1. public final class SystemServer {
  2. /**
  3. * The main entry point from zygote.
  4. */
  5. public static void main(String[] args) {
  6. new SystemServer().run();
  7. }
  8. private void run() {
  9. ......
  10. // 初始化虚拟机内存
  11. VMRuntime.getRuntime().clearGrowthLimit();
  12. VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
  13. //设置进程优先级,初始化 MainLooper
  14. android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND);
  15. android.os.Process.setCanSelfBackground(false);
  16. Looper.prepareMainLooper();
  17. // 加载 native services
  18. System.loadLibrary("android_servers");
  19. ......
  20. // 代码 1,初始化 System Context
  21. createSystemContext();
  22. // 代码 2,创建 SystemServiceManager 对象
  23. mSystemServiceManager = new SystemServiceManager(mSystemContext);
  24. mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
  25. LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
  26. // 为初始化任务准备线程池
  27. SystemServerInitThreadPool.get();
  28. // 代码 3,启动系统服务
  29. try {
  30. traceBeginAndSlog("StartServices");
  31. startBootstrapServices();
  32. startCoreServices();
  33. startOtherServices();
  34. SystemServerInitThreadPool.shutdown();
  35. } catch (Throwable ex) {
  36. Slog.e("System", "******************************************");
  37. Slog.e("System", "************ Failure starting system services", ex);
  38. throw ex;
  39. } finally {
  40. traceEnd();
  41. }
  42. ......
  43. Looper.loop();
  44. }
  45. }

从上面代码中可以看到,在 SystemServer 中也持有 Context 对象,这个 Context 真是无处不在。SystemServer 初始化过程中,我们主要分析三点:

  1. 代码 1 处初始化 SystemContext
  2. 代码 2 处创建 SystemServiceManager 对象
  3. 代码 3 处启动系统服务,共分为三种系统服务:系统引导服务(BootstrapServices)、核心服务(CoreServices)和其他服务(OtherServices)

2.2 初始化 SystemContext

首先来到 createSystemContext() 方法中,代码如下所示

  1. private void createSystemContext() {
  2. ActivityThread activityThread = ActivityThread.systemMain();
  3. mSystemContext = activityThread.getSystemContext();
  4. mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
  5. final Context systemUiContext = activityThread.getSystemUiContext();
  6. systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
  7. }

createSystemContext() 中调用 ActivityThread.systemMain() 创建了一个 ActivityThread 对象,并设置了此 ActivityThread 对象 SystemContext 和 SystemUIContext 的主题,接着我们看下 ActivityThread.systemMain() 方法

  1. public final class ActivityThread {
  2. ......
  3. public static ActivityThread systemMain() {
  4. // The system process on low-memory devices do not get to use hardware
  5. // accelerated drawing, since this can add too much overhead to the
  6. // process.
  7. if (!ActivityManager.isHighEndGfx()) {
  8. ThreadedRenderer.disable(true);
  9. } else {
  10. ThreadedRenderer.enableForegroundTrimming();
  11. }
  12. ActivityThread thread = new ActivityThread();
  13. thread.attach(true);
  14. return thread;
  15. }
  16. ......
  17. private void attach(boolean system) {
  18. sCurrentActivityThread = this;
  19. mSystemThread = system;
  20. if (!system) {
  21. // 非系统启动
  22. ......
  23. } else {
  24. // 通过 SystemServer 启动 ActivityThread 对象
  25. android.ddm.DdmHandleAppName.setAppName("system_process",
  26. UserHandle.myUserId());
  27. try {
  28. // 代码 1,创建 Instrumentation、Application、Context 对象
  29. mInstrumentation = new Instrumentation();
  30. ContextImpl context = ContextImpl.createAppContext(
  31. this, getSystemContext().mPackageInfo);
  32. mInitialApplication = context.mPackageInfo.makeApplication(true, null);
  33. mInitialApplication.onCreate();
  34. } catch (Exception e) {
  35. throw new RuntimeException(
  36. "Unable to instantiate Application():" + e.toString(), e);
  37. }
  38. }
  39. // 为 ViewRootImpl 设置配置更新回调,当系统资源配置(如:系统字体)发生变化时,通知系统配置发生变化
  40. ViewRootImpl.ConfigChangedCallback configChangedCallback
  41. = (Configuration globalConfig) -> {
  42. synchronized (mResourcesManager) {
  43. // We need to apply this change to the resources immediately, because upon returning
  44. // the view hierarchy will be informed about it.
  45. if (mResourcesManager.ap
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/204027
推荐阅读
相关标签
  

闽ICP备14008679号