基于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

相关推荐
拭心9 小时前
Google 提供的 Android 端上大模型组件:MediaPipe LLM 介绍
android
带电的小王11 小时前
WhisperKit: Android 端测试 Whisper -- Android手机(Qualcomm GPU)部署音频大模型
android·智能手机·whisper·qualcomm
梦想平凡11 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
元争栈道12 小时前
webview和H5来实现的android短视频(短剧)音视频播放依赖控件
android·音视频
阿甘知识库12 小时前
宝塔面板跨服务器数据同步教程:双机备份零停机
android·运维·服务器·备份·同步·宝塔面板·建站
元争栈道13 小时前
webview+H5来实现的android短视频(短剧)音视频播放依赖控件资源
android·音视频
MuYe13 小时前
Android Hook - 动态加载so库
android
居居飒14 小时前
Android学习(四)-Kotlin编程语言-for循环
android·学习·kotlin
Henry_He17 小时前
桌面列表小部件不能点击的问题分析
android
工程师老罗17 小时前
Android笔试面试题AI答之Android基础(1)
android