Android开机启动流程
systemReady启动"added application"
@frameworks/base/services/java/com/android/server/SystemServer.java
main
new SystemServer().run();
startBootstrapServices
mActivityManagerService = ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);
mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);
mActivityManagerService.setSystemProcess();
@frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
ServiceManager.addService(Context.ACTIVITY_SERVICE
ServiceManager.addService("meminfo", new MemBinder(this)
ServiceManager.addService("gfxinfo", new GraphicsBinder(this))
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo( "android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
ProcessRecord app = mProcessList.newProcessRecordLocked(info, info.processName,
startOtherServices
@frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
mActivityManagerService.systemReady(() -> {
mLocalDeviceIdleController = LocalServices.getService(DeviceIdleInternal.class);
@frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
mActivityTaskManager.onSystemReady();
@frameworks/base/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
mTaskSupervisor.onSystemReady();
@frameworks/base/services/core/java/com/android/server/wm/LaunchParamsPersister.java
mLaunchParamsPersister.onSystemReady();
PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class);
mPackageList = pmi.getPackageList(new PackageListObserver());
@frameworks/base/services/core/java/com/android/server/am/UserController.java
mUserController.onSystemReady();
@frameworks/base/services/core/java/com/android/server/appop/AppOpsService.java
mAppOpsService.systemReady();
mProcessList.onSystemReady();
mAppExitInfoTracker.onSystemReady();
ArrayList<ProcessRecord> procsToKill = null; //杀应用名单
for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
ProcessRecord proc = mPidsSelfLocked.valueAt(i);
procsToKill.add(proc);
@frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
startPersistentApps(PackageManager.MATCH_DIRECT_BOOT_AWARE);
final List<ApplicationInfo> apps = AppGlobals.getPackageManager().getPersistentApplications(STOCK_PM_FLAGS | matchFlags).getList();
addAppLocked(app, null, false, null
ProcessRecord app;
app = mProcessList.newProcessRecordLocked(info, customProcess, isolated, 0,
mProcessList.startProcessLocked(app, new HostingRecord("added application", customProcess != null ? customProcess : app.processName),
Start proc 1056:com.android.se/1068 for type:added application type end com.android.se
Start proc 1078:com.android.phone/1001 for type:added application type end com.android.phone
Start proc 1079:com.android.systemui/u0a50 for type:added application type end com.android.systemui
Home应用程序
Android系统在启动时安装应用程序的过程,这些应用程序安装好之后,还需要有一个Home应用程序来负责把它们在桌面上展示出来,在Android系统中,这个默认的Home应用程序就是Launcher了.
@frameworks/base/services/core/java/com/android/server/am/UserController.java
finishUserUnlocking
mHandler.obtainMessage(USER_UNLOCK_MSG, userId, 0, uss).sendToTarget();
case USER_UNLOCK_MSG: finishUserUnlocked
finishUserUnlockedCompleted
mHandler.obtainMessage(USER_UNLOCKED_MSG, userId, 0).sendToTarget();
case USER_UNLOCKED_MSG: mInjector.getSystemServiceManager().onUserUnlocked(msg.arg1);
mService.mTaskSupervisor.onUserUnlocked(user.getUserIdentifier());
@frameworks/base/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
scheduleStartHome("userUnlocked");
mHandler.obtainMessage(START_HOME_MSG, reason).sendToTarget();
case START_HOME_MSG: { mRootWindowContainer.startHomeOnEmptyDisplays((String) msg.obj);
@frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java
startHomeOnEmptyDisplays
forAllTaskDisplayAreas(taskDisplayArea -> {
startHomeOnTaskDisplayArea(mCurrentUser, myReason, taskDisplayArea,
@frameworks/base/services/core/java/com/android/server/wm/ActivityStartController.java
Service.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason, taskDisplayArea);
@frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java
mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason).setActivityOptions(options.toBundle()).execute();
res = executeRequest(mRequest);
ActivityRecord r = new ActivityRecord.Builder(mService)..setActivityInfo(aInfo)
mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,startFlags, doResume, options, inTask, inTaskFragment, restrictedBgActivity,
mTargetRootTask.startActivityLocked(mStartActivity,
@frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java
mRootWindowContainer.resumeFocusedTasksTopActivities(mTargetRootTask, mStartActivity, mOptions, mTransientLaunch);
final Task focusedRoot = display.getFocusedRootTask();
@frameworks/base/services/core/java/com/android/server/wm/Task.java
focusedRoot.resumeTopActivityUncheckedLocked(target, targetOptions);
someActivityResumed = resumeTopActivityInnerLocked(prev, options, deferPause);
@frameworks/base/services/core/java/com/android/server/wm/TaskFragment.java
resumed[0] = topFragment.resumeTopActivity(prev, options, deferPause);
ActivityRecord next = topRunningActivity(true /* focusableOnly */);
pausing |= startPausing(mTaskSupervisor.mUserLeaving, false /* uiSleeping */, next, "resumeTopActivity"); //这里就是stop上一个activity的地方
if (next.attachedToProcess()) { //要启动的activity所在的进程已经起来了,直接通过事务的形式去启动activity
final ClientTransaction transaction = ClientTransaction.obtain(next.app.getThread(), next.appToken);
mAtmService.getLifecycleManager().scheduleTransaction(transaction);
} else { //如果进程没启动,调用ActivityTaskSupervisor的startSpecificActivity
@frameworks/base/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
mTaskSupervisor.startSpecificActivity(next, true, true);
@base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
mService.startProcessAsync(r, knownToBeDead, isTop, isTop ? "top-activity" : "activity");
final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess, mAmInternal, activity.processName,
mH.sendMessage(m);
LocalService::startProcess //class LocalService extends ActivityManagerInternal
startProcessLocked(processName, info, knownToBeDead, 0 /* intentFlags */, new HostingRecord(hostingType, hostingName, isTop),
@frameworks/base/services/core/java/com/android/server/am/ProcessList.java
return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,
startProcessLocked(app, hostingRecord, zygotePolicyFlags, abiOverride);
startProcessLocked(app, hostingRecord, zygotePolicyFlags, false
startProcessLocked(hostingRecord, entryPoint, app, uid, gids, runtimeFlags, zygotePolicyFlags, mountExternal, seInfo, requiredAbi,
mService.mProcStartHandler.post(() -> handleProcessStart( app,
handleProcessStartedLocked(app, startResult, startSeq);
return handleProcessStartedLocked(pending, startResult.pid, startResult.usingWrapper,
buf.append("Start proc "); //Start proc 1486:com.dynabook.VDlauncher/1000 for top-activity {com.dynabook.VDlauncher/com.dynabook.VDlauncher.MainActivity}
buf.append(pid);
buf.append(':');
buf.append(app.processName);
buf.append('/');
buf.append(app.getHostingRecord().getType());
buf.append(app.getHostingRecord().getName());
..............
@src/main/java/com/xxx/launcher/MainActivity.java
MainActivity::onCreate
@frameworks/base/services/java/com/android/server/SystemServer.java
main
new SystemServer().run();
startBootstrapServices
mActivityManagerService = ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);
mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);
mActivityManagerService.setSystemProcess();
@frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
ServiceManager.addService(Context.ACTIVITY_SERVICE
ServiceManager.addService("meminfo", new MemBinder(this)
ServiceManager.addService("gfxinfo", new GraphicsBinder(this))
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo( "android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
ProcessRecord app = mProcessList.newProcessRecordLocked(info, info.processName,
startOtherServices
@frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
mActivityManagerService.systemReady(() -> {
mLocalDeviceIdleController = LocalServices.getService(DeviceIdleInternal.class);
@frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
mActivityTaskManager.onSystemReady();
@frameworks/base/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
mTaskSupervisor.onSystemReady();
@frameworks/base/services/core/java/com/android/server/wm/LaunchParamsPersister.java
mLaunchParamsPersister.onSystemReady();
PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class);
mPackageList = pmi.getPackageList(new PackageListObserver());
@frameworks/base/services/core/java/com/android/server/am/UserController.java
mUserController.onSystemReady();
@frameworks/base/services/core/java/com/android/server/appop/AppOpsService.java
mAppOpsService.systemReady();
mProcessList.onSystemReady();
mAppExitInfoTracker.onSystemReady();
ArrayList<ProcessRecord> procsToKill = null; //杀应用名单
for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
ProcessRecord proc = mPidsSelfLocked.valueAt(i);
procsToKill.add(proc);
@frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
startPersistentApps(PackageManager.MATCH_DIRECT_BOOT_AWARE);
final List<ApplicationInfo> apps = AppGlobals.getPackageManager().getPersistentApplications(STOCK_PM_FLAGS | matchFlags).getList();
addAppLocked(app, null, false, null
ProcessRecord app;
app = mProcessList.newProcessRecordLocked(info, customProcess, isolated, 0,
mProcessList.startProcessLocked(app, new HostingRecord("added application", customProcess != null ? customProcess : app.processName),
Start proc 1056:com.android.se/1068 for type:added application type end com.android.se
Start proc 1078:com.android.phone/1001 for type:added application type end com.android.phone
Start proc 1079:com.android.systemui/u0a50 for type:added application type end com.android.systemui
@frameworks/base/services/core/java/com/android/server/am/UserController.java
finishUserUnlocking
mHandler.obtainMessage(USER_UNLOCK_MSG, userId, 0, uss).sendToTarget();
case USER_UNLOCK_MSG: finishUserUnlocked
finishUserUnlockedCompleted
mHandler.obtainMessage(USER_UNLOCKED_MSG, userId, 0).sendToTarget();
case USER_UNLOCKED_MSG: mInjector.getSystemServiceManager().onUserUnlocked(msg.arg1);
mService.mTaskSupervisor.onUserUnlocked(user.getUserIdentifier());
@frameworks/base/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
scheduleStartHome("userUnlocked");
mHandler.obtainMessage(START_HOME_MSG, reason).sendToTarget();
case START_HOME_MSG: { mRootWindowContainer.startHomeOnEmptyDisplays((String) msg.obj);
@frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java
startHomeOnEmptyDisplays
forAllTaskDisplayAreas(taskDisplayArea -> {
startHomeOnTaskDisplayArea(mCurrentUser, myReason, taskDisplayArea,
@frameworks/base/services/core/java/com/android/server/wm/ActivityStartController.java
Service.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason, taskDisplayArea);
@frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java
mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason).setActivityOptions(options.toBundle()).execute();
res = executeRequest(mRequest);
ActivityRecord r = new ActivityRecord.Builder(mService)..setActivityInfo(aInfo)
mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,startFlags, doResume, options, inTask, inTaskFragment, restrictedBgActivity,
mTargetRootTask.startActivityLocked(mStartActivity,
@frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java
mRootWindowContainer.resumeFocusedTasksTopActivities(mTargetRootTask, mStartActivity, mOptions, mTransientLaunch);
final Task focusedRoot = display.getFocusedRootTask();
@frameworks/base/services/core/java/com/android/server/wm/Task.java
focusedRoot.resumeTopActivityUncheckedLocked(target, targetOptions);
someActivityResumed = resumeTopActivityInnerLocked(prev, options, deferPause);
@frameworks/base/services/core/java/com/android/server/wm/TaskFragment.java
resumed[0] = topFragment.resumeTopActivity(prev, options, deferPause);
ActivityRecord next = topRunningActivity(true /* focusableOnly */);
pausing |= startPausing(mTaskSupervisor.mUserLeaving, false /* uiSleeping */, next, "resumeTopActivity"); //这里就是stop上一个activity的地方
if (next.attachedToProcess()) { //要启动的activity所在的进程已经起来了,直接通过事务的形式去启动activity
final ClientTransaction transaction = ClientTransaction.obtain(next.app.getThread(), next.appToken);
mAtmService.getLifecycleManager().scheduleTransaction(transaction);
} else { //如果进程没启动,调用ActivityTaskSupervisor的startSpecificActivity
@frameworks/base/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
mTaskSupervisor.startSpecificActivity(next, true, true);
@base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
mService.startProcessAsync(r, knownToBeDead, isTop, isTop ? "top-activity" : "activity");
final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess, mAmInternal, activity.processName,
mH.sendMessage(m);
LocalService::startProcess //class LocalService extends ActivityManagerInternal
startProcessLocked(processName, info, knownToBeDead, 0 /* intentFlags */, new HostingRecord(hostingType, hostingName, isTop),
@frameworks/base/services/core/java/com/android/server/am/ProcessList.java
return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,
startProcessLocked(app, hostingRecord, zygotePolicyFlags, abiOverride);
startProcessLocked(app, hostingRecord, zygotePolicyFlags, false
startProcessLocked(hostingRecord, entryPoint, app, uid, gids, runtimeFlags, zygotePolicyFlags, mountExternal, seInfo, requiredAbi,
mService.mProcStartHandler.post(() -> handleProcessStart( app,
handleProcessStartedLocked(app, startResult, startSeq);
return handleProcessStartedLocked(pending, startResult.pid, startResult.usingWrapper,
buf.append("Start proc "); //Start proc 1486:com.dynabook.VDlauncher/1000 for top-activity {com.dynabook.VDlauncher/com.dynabook.VDlauncher.MainActivity}
buf.append(pid);
buf.append(':');
buf.append(app.processName);
buf.append('/');
buf.append(app.getHostingRecord().getType());
buf.append(app.getHostingRecord().getName());
..............
@src/main/java/com/xxx/launcher/MainActivity.java
MainActivity::onCreate