赞
踩
ActivityManagerService(以下简称为 AMS)是 Android 中最核心的系统服务之一,我认为 AMS 最重要的功能有两个:
这篇文章对 Android 8.0 系统中的 AMS 启动流程加以分析。
首先,我们来看一下 AMS 中管理应用程序进程和应用程序进程中四大组件的相关类和他们的关系图
ActivityThread 是应用程序进程的入口,负责对应用程序进程中主线程的管理,被 AMS 调度从而直接调用四大组件的生命周期方法
在 AMS 中并不能直接管理四大组件,四大组件在 AMS 中都有一个对应的类,AMS 中管理的是 ActivityRecord 等对象,再通过 Binder 通信向 ApplicationThread 发送消息,ApplicationThread 再通过 Handler 将消息发送到主线程中,最后就会去调用四大组件的生命周期方法
应用程序进程中的四大组件 | AMS 中的四大组件 |
---|---|
Activity | ActivityRecord |
Service | ServiceRecord |
Broadcast | BroadcastRecord |
ContentProvider | ContentProviderRecord |
接下来,我们分析下 AMS 的启动流程
和之前分析 WMS 的启动一样,从 SystemServer 中开始分析。
2.1 SystemServer 初始化
- public final class SystemServer {
-
- /**
- * The main entry point from zygote.
- */
- public static void main(String[] args) {
- new SystemServer().run();
- }
-
- private void run() {
- ......
- // 初始化虚拟机内存
- VMRuntime.getRuntime().clearGrowthLimit();
- VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
-
- //设置进程优先级,初始化 MainLooper
- android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND);
- android.os.Process.setCanSelfBackground(false);
- Looper.prepareMainLooper();
-
- // 加载 native services
- System.loadLibrary("android_servers");
- ......
- // 代码 1,初始化 System Context
- createSystemContext();
-
- // 代码 2,创建 SystemServiceManager 对象
- mSystemServiceManager = new SystemServiceManager(mSystemContext);
- mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
- LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
- // 为初始化任务准备线程池
- SystemServerInitThreadPool.get();
-
- // 代码 3,启动系统服务
- try {
- traceBeginAndSlog("StartServices");
- startBootstrapServices();
- startCoreServices();
- startOtherServices();
- SystemServerInitThreadPool.shutdown();
- } catch (Throwable ex) {
- Slog.e("System", "******************************************");
- Slog.e("System", "************ Failure starting system services", ex);
- throw ex;
- } finally {
- traceEnd();
- }
- ......
- Looper.loop();
- }
- }

从上面代码中可以看到,在 SystemServer 中也持有 Context 对象,这个 Context 真是无处不在。SystemServer 初始化过程中,我们主要分析三点:
2.2 初始化 SystemContext
首先来到 createSystemContext()
方法中,代码如下所示
- private void createSystemContext() {
- ActivityThread activityThread = ActivityThread.systemMain();
- mSystemContext = activityThread.getSystemContext();
- mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
-
- final Context systemUiContext = activityThread.getSystemUiContext();
- systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
- }
在 createSystemContext()
中调用 ActivityThread.systemMain()
创建了一个 ActivityThread 对象,并设置了此 ActivityThread 对象 SystemContext 和 SystemUIContext 的主题,接着我们看下 ActivityThread.systemMain()
方法
- public final class ActivityThread {
-
- ......
-
- public static ActivityThread systemMain() {
- // The system process on low-memory devices do not get to use hardware
- // accelerated drawing, since this can add too much overhead to the
- // process.
- if (!ActivityManager.isHighEndGfx()) {
- ThreadedRenderer.disable(true);
- } else {
- ThreadedRenderer.enableForegroundTrimming();
- }
- ActivityThread thread = new ActivityThread();
- thread.attach(true);
- return thread;
- }
-
- ......
-
- private void attach(boolean system) {
- sCurrentActivityThread = this;
- mSystemThread = system;
- if (!system) {
- // 非系统启动
- ......
- } else {
- // 通过 SystemServer 启动 ActivityThread 对象
- android.ddm.DdmHandleAppName.setAppName("system_process",
- UserHandle.myUserId());
- try {
- // 代码 1,创建 Instrumentation、Application、Context 对象
- mInstrumentation = new Instrumentation();
- ContextImpl context = ContextImpl.createAppContext(
- this, getSystemContext().mPackageInfo);
- mInitialApplication = context.mPackageInfo.makeApplication(true, null);
- mInitialApplication.onCreate();
- } catch (Exception e) {
- throw new RuntimeException(
- "Unable to instantiate Application():" + e.toString(), e);
- }
- }
-
- // 为 ViewRootImpl 设置配置更新回调,当系统资源配置(如:系统字体)发生变化时,通知系统配置发生变化
- ViewRootImpl.ConfigChangedCallback configChangedCallback
- = (Configuration globalConfig) -> {
- synchronized (mResourcesManager) {
- // We need to apply this change to the resources immediately, because upon returning
- // the view hierarchy will be informed about it.
- if (mResourcesManager.ap

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。