跳转至

应用启动流程

1. 桌面应用的启动

我们每天看到的桌面实际上也是一个 App,aosp 中默认为 Launcher3,位于 /packages/apps/Launcher3/

SystemServer.runstartOtherServices 中会进入 ActivityManagerService.systemReady 从而调用 mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");

/frameworks/base/services/java/com/android/server/SystemServer.java
// line 796-797
    private void run() {
        TimingsTraceAndSlog t = new TimingsTraceAndSlog();
//...
// line 984-994 调用 startOtherServices 方法
        try {
            t.traceBegin("StartServices");
            startBootstrapServices(t);
            startCoreServices(t);
            startOtherServices(t);
            startApexServices(t);
            // Only update the timeout after starting all the services so that we use
            // the default timeout to start system server.
            updateWatchdogTimeout(t);
            CriticalEventLog.getInstance().logSystemServerStarted();
        } catch (Throwable ex) {
/frameworks/base/services/java/com/android/server/SystemServer.java
// line 1513-1518
    /**
     * Starts a miscellaneous grab bag of stuff that has yet to be refactored and organized.
     */
    private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
        t.traceBegin("startOtherServices");
        mSystemServiceManager.updateOtherServicesStartIndex();
//...
// line 3217-3223 调用 mActivityManagerService.systemReady
        // We now tell the activity manager it is okay to run third party
        // code.  It will call back into us once it has gotten to the state
        // where third party code can really run (but before it has actually
        // started launching the initial applications), for us to complete our
        // initialization.
        mActivityManagerService.systemReady(() -> {
            Slog.i(TAG, "Making services ready");
/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
// line 8970-8975
    /**
     * Ready. Set. Go!
     */
    public void systemReady(final Runnable goingCallback, @NonNull TimingsTraceAndSlog t) {
        t.traceBegin("PhaseActivityManagerReady");
        mSystemServiceManager.preSystemReady();
//...
// line 9129-9139 调用 mAtmInternal.startHomeOnAllDisplays
            boolean isBootingSystemUser = currentUserId == UserHandle.USER_SYSTEM;

            // Some systems - like automotive - will explicitly unlock system user then switch
            // to a secondary user.
            // TODO(b/266158156): this workaround shouldn't be necessary once we move
            // the headless-user start logic to UserManager-land.
            if (isBootingSystemUser && !UserManager.isHeadlessSystemUserMode()) {
                t.traceBegin("startHomeOnAllDisplays");
                mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");
                t.traceEnd();
            }

这里调用的是 ActivityTaskManagerInternal。它是系统进程内部使用的抽象接口,真正的实现是 ActivityTaskManagerService 内部的 LocalService。AMS 通过 LocalServices.getService(ActivityTaskManagerInternal.class) 拿到这个内部服务,因此这里最终会进入 ActivityTaskManagerService.LocalService.startHomeOnAllDisplays()

/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
1
2
3
4
5
6
7
8
9
// line 6038
    final class LocalService extends ActivityTaskManagerInternal {
// line 6636-6641
        @Override
        public boolean startHomeOnAllDisplays(int userId, String reason) {
            synchronized (mGlobalLock) {
                return mRootWindowContainer.startHomeOnAllDisplays(userId, reason);
            }
        }

在这里进入了 RootWindowContainerstartHomeOnAllDisplays