基于Android13源码分析Launcher启动

AMS被SystemServer启动之后,SystemServer会通过AMS调用startHomeActivity启动Launcher. Launcher其实就是个Activity,学习Launcher的启动后,再去看Activity启动,会容易很多。

Launcher的启动分2个阶段:

第一阶段是startHomeActivity,触发activity的进程创建;

第二阶段就是AMS的attachApplication在进程创建后被调用,最终执行realStartActivityLocked会回调Activity的onCreate函数。

java 复制代码
class ActivityManagerService{
    public void systemReady(final Runnable goingCallback, @NonNull TimingsTraceAndSlog t) {
    ...
    mAtmInternal.resumeTopActivities(false /* scheduleIdle */);
    ...
    }    
}


class ActivityTaskManagerService{

          @Override
          public void resumeTopActivities(boolean scheduleIdle) {

              synchronized (mGlobalLock) {

                  mRootWindowContainer.resumeFocusedTasksTopActivities();

                  if (scheduleIdle) {

                      mTaskSupervisor.scheduleIdle();

                  }

              }

          }
}

class RootWindowContainer{

    boolean resumeFocusedTasksTopActivities(Task targetRootTask, ActivityRecord target, ActivityOptions targetOptions,boolean deferPause) {
       
    for (int displayNdx = getChildCount() - 1; displayNdx >= 0; --displayNdx) {

        ...
        result |= resumedOnDisplay[0];

        if (!resumedOnDisplay[0]) {
            if (focusedRoot != null) {

                result |= focusedRoot.resumeTopActivityUncheckedLocked(target, targetOptions);

             } else if (targetRootTask == null) {

                result |= resumeHomeActivity(null /* prev */, "no-focusable-task",

                              display.getDefaultTaskDisplayArea());

            }

        }
    }

}



    boolean startHomeOnTaskDisplayArea(int userId, String reason, TaskDisplayArea taskDisplayArea, boolean allowInstrumenting, boolean fromHomeKey) {

         Intent homeIntent = null;

          ActivityInfo aInfo = null;

          if (taskDisplayArea == getDefaultTaskDisplayArea()) {

              homeIntent = mService.getHomeIntent();

              aInfo = resolveHomeActivity(userId, homeIntent);

          } else if (shouldPlaceSecondaryHomeOnDisplayArea(taskDisplayArea)) {

              Pair<ActivityInfo, Intent> info = resolveSecondaryHomeActivity(userId, taskDisplayArea);

              aInfo = info.first;

              homeIntent = info.second;

          }

          if (aInfo == null || homeIntent == null) {

              return false;

          }

  

          if (!canStartHomeOnDisplayArea(aInfo, taskDisplayArea, allowInstrumenting)) {

              return false;

          }

  

          // Updates the home component of the intent.

          homeIntent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));

          homeIntent.setFlags(homeIntent.getFlags() | FLAG_ACTIVITY_NEW_TASK);

          // Updates the extra information of the intent.

          if (fromHomeKey) {

              homeIntent.putExtra(WindowManagerPolicy.EXTRA_FROM_HOME_KEY, true);

              if (mWindowManager.getRecentsAnimationController() != null) {

                  mWindowManager.getRecentsAnimationController().cancelAnimationForHomeStart();

              }

          }

          homeIntent.putExtra(WindowManagerPolicy.EXTRA_START_REASON, reason);

          // Update the reason for ANR debugging to verify if the user activity is the one that

          // actually launched.

          final String myReason = reason + ":" + userId + ":" + UserHandle.getUserId(aInfo.applicationInfo.uid) + ":" + taskDisplayArea.getDisplayId();

          mService.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason, taskDisplayArea);

          return true;
    }
}

在线源码:

1 Launcher

2 ActivityManagerService

相关推荐
奔跑吧 android1 分钟前
【android bluetooth 框架分析 02】【Module详解 6】【StorageModule 模块介绍】
android·bluetooth·bt·aosp13·storagemodule
田一一一4 小时前
Android framework 中间件开发(三)
android·中间件·framework·jni
androidwork8 小时前
掌握 Kotlin Android 单元测试:MockK 框架深度实践指南
android·kotlin
田一一一8 小时前
Android framework 中间件开发(二)
android·中间件·framework
追随远方9 小时前
FFmpeg在Android开发中的核心价值是什么?
android·ffmpeg
神探阿航10 小时前
HNUST湖南科技大学-安卓Android期中复习
android·安卓·hnust
千里马-horse12 小时前
android vlc播放rtsp
android·media·rtsp·mediaplayer·vlc
難釋懷12 小时前
Android开发-文本输入
android·gitee
志存高远6614 小时前
(面试)Android各版本新特性
android
IT从业者张某某14 小时前
信奥赛-刷题笔记-队列篇-T3-P3662Why Did the Cow Cross the Road II S
android·笔记