基于Android13源码分析Launcher启动

[复制链接]
发表于 2026-1-2 12:19:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
        AMS被SystemServer启动之后,SystemServer会通过AMS调用startHomeActivity启动Launcher. Launcher着实就是个Activity,学习Launcher的启动后,再去看Activity启动,会容易许多。
Launcher的启动分2个阶段:
第一阶段是startHomeActivity,触发activity的历程创建;
第二阶段就是AMS的attachApplication在历程创建后被调用,终极实行realStartActivityLocked会回调Activity的onCreate函数。
  1. class ActivityManagerService{
  2.     public void systemReady(final Runnable goingCallback, @NonNull TimingsTraceAndSlog t) {
  3.     ...
  4.     mAtmInternal.resumeTopActivities(false /* scheduleIdle */);
  5.     ...
  6.     }   
  7. }
  8. class ActivityTaskManagerService{
  9.           @Override
  10.           public void resumeTopActivities(boolean scheduleIdle) {
  11.               synchronized (mGlobalLock) {
  12.                   mRootWindowContainer.resumeFocusedTasksTopActivities();
  13.                   if (scheduleIdle) {
  14.                       mTaskSupervisor.scheduleIdle();
  15.                   }
  16.               }
  17.           }
  18. }
  19. class RootWindowContainer{
  20.     boolean resumeFocusedTasksTopActivities(Task targetRootTask, ActivityRecord target, ActivityOptions targetOptions,boolean deferPause) {
  21.       
  22.     for (int displayNdx = getChildCount() - 1; displayNdx >= 0; --displayNdx) {
  23.         ...
  24.         result |= resumedOnDisplay[0];
  25.         if (!resumedOnDisplay[0]) {
  26.             if (focusedRoot != null) {
  27.                 result |= focusedRoot.resumeTopActivityUncheckedLocked(target, targetOptions);
  28.              } else if (targetRootTask == null) {
  29.                 result |= resumeHomeActivity(null /* prev */, "no-focusable-task",
  30.                               display.getDefaultTaskDisplayArea());
  31.             }
  32.         }
  33.     }
  34. }
  35.     boolean startHomeOnTaskDisplayArea(int userId, String reason, TaskDisplayArea taskDisplayArea, boolean allowInstrumenting, boolean fromHomeKey) {
  36.          Intent homeIntent = null;
  37.           ActivityInfo aInfo = null;
  38.           if (taskDisplayArea == getDefaultTaskDisplayArea()) {
  39.               homeIntent = mService.getHomeIntent();
  40.               aInfo = resolveHomeActivity(userId, homeIntent);
  41.           } else if (shouldPlaceSecondaryHomeOnDisplayArea(taskDisplayArea)) {
  42.               Pair<ActivityInfo, Intent> info = resolveSecondaryHomeActivity(userId, taskDisplayArea);
  43.               aInfo = info.first;
  44.               homeIntent = info.second;
  45.           }
  46.           if (aInfo == null || homeIntent == null) {
  47.               return false;
  48.           }
  49.   
  50.           if (!canStartHomeOnDisplayArea(aInfo, taskDisplayArea, allowInstrumenting)) {
  51.               return false;
  52.           }
  53.   
  54.           // Updates the home component of the intent.
  55.           homeIntent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
  56.           homeIntent.setFlags(homeIntent.getFlags() | FLAG_ACTIVITY_NEW_TASK);
  57.           // Updates the extra information of the intent.
  58.           if (fromHomeKey) {
  59.               homeIntent.putExtra(WindowManagerPolicy.EXTRA_FROM_HOME_KEY, true);
  60.               if (mWindowManager.getRecentsAnimationController() != null) {
  61.                   mWindowManager.getRecentsAnimationController().cancelAnimationForHomeStart();
  62.               }
  63.           }
  64.           homeIntent.putExtra(WindowManagerPolicy.EXTRA_START_REASON, reason);
  65.           // Update the reason for ANR debugging to verify if the user activity is the one that
  66.           // actually launched.
  67.           final String myReason = reason + ":" + userId + ":" + UserHandle.getUserId(aInfo.applicationInfo.uid) + ":" + taskDisplayArea.getDisplayId();
  68.           mService.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason, taskDisplayArea);
  69.           return true;
  70.     }
  71. }
复制代码


在线源码:
1 Launcher
2 ActivityManagerService
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表