Home AMS分析
Post
Cancel

AMS分析

简介

源码使用官方 Android29

ActivityManagerService是Android提供的一个用于管理Activity运行状态的系统进程

启动过程

启动首先是从SystemService的main方法开始启动的

1
2
3
4
5
6
/**
    * The main entry point from zygote.
    */
public static void main(String[] args) {
    new SystemServer().run();
}

从官方注射可以看到,这个是由zygote启动的服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
private void run() {
    TimingsTraceAndSlog t = new TimingsTraceAndSlog();
    try {
        
        // ...

        // 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);

        SystemServiceRegistry.sEnableServiceNotFoundWtf = true;

        // Initialize native services.
        System.loadLibrary("android_servers");

        // Allow heap / perf profiling.
        initZygoteChildHeapProfiling();

        //...

        // Create the system service manager.
        mSystemServiceManager = new SystemServiceManager(mSystemContext);
        mSystemServiceManager.setStartInfo(mRuntimeRestart, mRuntimeStartElapsedTime, mRuntimeStartUptime);

        LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);

    } finally {
        t.traceEnd();  // InitBeforeStartServices
    }

    // Setup the default WTF handler
    RuntimeInit.setDefaultApplicationWtfHandler(SystemServer::handleEarlySystemWtf);

    // Start services.
    try {
        t.traceBegin("StartServices");
        // 启动很多Service
        startBootstrapServices(t);
        // 启动很多核心的系统服务
        startCoreServices(t);
        startOtherServices(t);
    } catch (Throwable ex) {
        Slog.e("System", "******************************************");
        Slog.e("System", "************ Failure starting system services", ex);
        throw ex;
    } finally {
        t.traceEnd(); // StartServices
    }

    StrictMode.initVmDefaults(null);

    if (!mRuntimeRestart && !isFirstBootOrUpgrade()) {
        final long uptimeMillis = SystemClock.elapsedRealtime();
        FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,
                FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__SYSTEM_SERVER_READY,
                uptimeMillis);
        final long maxUptimeMillis = 60 * 1000;
        if (uptimeMillis > maxUptimeMillis) {
            Slog.wtf(SYSTEM_SERVER_TIMING_TAG,
                    "SystemServer init took too long. uptimeMillis=" + uptimeMillis);
        }
    }

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

SystemService启动后也是利用的Handler进行循环处理,期间使用SystemServiceManager启动了很多系统服务,例如Activity栈管理、系统配置、电源管理等。

AMS触发Activity生命周期

AMS对Activity的生命周期分发主要通过ClientTransaction携带不同的生命周期方法,例如LaunchActivityItem、ResumeActivityItem、PauseActivityItem等来实现的。

Acitity栈

在AMS上使用ActivityDisplay、ActivityStack、TaskRecord、ActivityRecord等来管理不同进程以及屏幕显示的Activity

// TODO 编译调试不同情况下ASM的调用过程

This post is licensed under CC BY 4.0 by the author.