> 文章列表 > android framework-ActivityManagerService(AMS)上

android framework-ActivityManagerService(AMS)上

android framework-ActivityManagerService(AMS)上

在这里插入图片描述

一、SystemServer

android-10.0.0_r41\\frameworks\\base\\services\\java\\com\\android\\server\\SystemServer.java

1.1、startOtherService

AMS初始化完成后,会调用systemReady方法

 mActivityManagerService.systemReady(() -> {Slog.i(TAG, "Making services ready");traceBeginAndSlog("StartActivityManagerReadyPhase");mSystemServiceManager.startBootPhase(SystemService.PHASE_ACTIVITY_MANAGER_READY);traceEnd();traceBeginAndSlog("StartObservingNativeCrashes");try {mActivityManagerService.startObservingNativeCrashes();} catch (Throwable e) {reportWtf("observing native crashes", e);}traceEnd();// No dependency on Webview preparation in system server. But this should// be completed before allowing 3rd partyfinal String WEBVIEW_PREPARATION = "WebViewFactoryPreparation";Future<?> webviewPrep = null;if (!mOnlyCore && mWebViewUpdateService != null) {webviewPrep = SystemServerInitThreadPool.get().submit(() -> {Slog.i(TAG, WEBVIEW_PREPARATION);TimingsTraceLog traceLog = new TimingsTraceLog(SYSTEM_SERVER_TIMING_ASYNC_TAG, Trace.TRACE_TAG_SYSTEM_SERVER);traceLog.traceBegin(WEBVIEW_PREPARATION);ConcurrentUtils.waitForFutureNoInterrupt(mZygotePreload, "Zygote preload");mZygotePreload = null;mWebViewUpdateService.prepareWebViewInSystemServer();traceLog.traceEnd();}, WEBVIEW_PREPARATION);}if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {traceBeginAndSlog("StartCarServiceHelperService");mSystemServiceManager.startService(CAR_SERVICE_HELPER_SERVICE_CLASS);traceEnd();}traceBeginAndSlog("StartSystemUI");try {startSystemUi(context, windowManagerF);} catch (Throwable e) {reportWtf("starting System UI", e);}traceEnd();// Enable airplane mode in safe mode. setAirplaneMode() cannot be called// earlier as it sends broadcasts to other services.// TODO: This may actually be too late if radio firmware already started leaking// RF before the respective services start. However, fixing this requires changes// to radio firmware and interfaces.if (safeMode) {traceBeginAndSlog("EnableAirplaneModeInSafeMode");try {connectivityF.setAirplaneMode(true);} catch (Throwable e) {reportWtf("enabling Airplane Mode during Safe Mode bootup", e);}traceEnd();}traceBeginAndSlog("MakeNetworkManagementServiceReady");try {if (networkManagementF != null) {networkManagementF.systemReady();}} catch (Throwable e) {reportWtf("making Network Managment Service ready", e);}CountDownLatch networkPolicyInitReadySignal = null;if (networkPolicyF != null) {networkPolicyInitReadySignal = networkPolicyF.networkScoreAndNetworkManagementServiceReady();}traceEnd();traceBeginAndSlog("MakeIpSecServiceReady");try {if (ipSecServiceF != null) {ipSecServiceF.systemReady();}} catch (Throwable e) {reportWtf("making IpSec Service ready", e);}traceEnd();traceBeginAndSlog("MakeNetworkStatsServiceReady");try {if (networkStatsF != null) {networkStatsF.systemReady();}} catch (Throwable e) {reportWtf("making Network Stats Service ready", e);}traceEnd();traceBeginAndSlog("MakeConnectivityServiceReady");try {if (connectivityF != null) {connectivityF.systemReady();}} catch (Throwable e) {reportWtf("making Connectivity Service ready", e);}traceEnd();traceBeginAndSlog("MakeNetworkPolicyServiceReady");try {if (networkPolicyF != null) {networkPolicyF.systemReady(networkPolicyInitReadySignal);}} catch (Throwable e) {reportWtf("making Network Policy Service ready", e);}traceEnd();// Wait for all packages to be preparedmPackageManagerService.waitForAppDataPrepared();// It is now okay to let the various system services start their// third party code...traceBeginAndSlog("PhaseThirdPartyAppsCanStart");// confirm webview completion before starting 3rd partyif (webviewPrep != null) {ConcurrentUtils.waitForFutureNoInterrupt(webviewPrep, WEBVIEW_PREPARATION);}mSystemServiceManager.startBootPhase(SystemService.PHASE_THIRD_PARTY_APPS_CAN_START);traceEnd();traceBeginAndSlog("StartNetworkStack");try {// Note : the network stack is creating on-demand objects that need to send// broadcasts, which means it currently depends on being started after// ActivityManagerService.mSystemReady and ActivityManagerService.mProcessesReady// are set to true. Be careful if moving this to a different place in the// startup sequence.NetworkStackClient.getInstance().start(context);} catch (Throwable e) {reportWtf("starting Network Stack", e);}traceEnd();traceBeginAndSlog("MakeLocationServiceReady");try {if (locationF != null) {locationF.systemRunning();}} catch (Throwable e) {reportWtf("Notifying Location Service running", e);}traceEnd();traceBeginAndSlog("MakeCountryDetectionServiceReady");try {if (countryDetectorF != null) {countryDetectorF.systemRunning();}} catch (Throwable e) {reportWtf("Notifying CountryDetectorService running", e);}traceEnd();traceBeginAndSlog("MakeNetworkTimeUpdateReady");try {if (networkTimeUpdaterF != null) {networkTimeUpdaterF.systemRunning();}} catch (Throwable e) {reportWtf("Notifying NetworkTimeService running", e);}traceEnd();traceBeginAndSlog("MakeInputManagerServiceReady");try {// TODO(BT) Pass parameter to input managerif (inputManagerF != null) {inputManagerF.systemRunning();}} catch (Throwable e) {reportWtf("Notifying InputManagerService running", e);}traceEnd();traceBeginAndSlog("MakeTelephonyRegistryReady");try {if (telephonyRegistryF != null) {telephonyRegistryF.systemRunning();}} catch (Throwable e) {reportWtf("Notifying TelephonyRegistry running", e);}traceEnd();traceBeginAndSlog("MakeMediaRouterServiceReady");try {if (mediaRouterF != null) {mediaRouterF.systemRunning();}} catch (Throwable e) {reportWtf("Notifying MediaRouterService running", e);}traceEnd();traceBeginAndSlog("MakeMmsServiceReady");try {if (mmsServiceF != null) {mmsServiceF.systemRunning();}} catch (Throwable e) {reportWtf("Notifying MmsService running", e);}traceEnd();traceBeginAndSlog("IncidentDaemonReady");try {// TODO: Switch from checkService to getService once it's always// in the build and should reliably be there.final IIncidentManager incident = IIncidentManager.Stub.asInterface(ServiceManager.getService(Context.INCIDENT_SERVICE));if (incident != null) {incident.systemRunning();}} catch (Throwable e) {reportWtf("Notifying incident daemon running", e);}traceEnd();}, BOOT_TIMINGS_TRACE_LOG);

二、ActivityManagerService

android-10.0.0_r41\\frameworks\\base\\services\\core\\java\\com\\android\\server\\am\\ActivityManagerService.java

2.1、systemReady

public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {traceLog.traceBegin("PhaseActivityManagerReady");synchronized(this) {if (mSystemReady) {// If we're done calling all the receivers, run the next "boot phase" passed in// by the SystemServerif (goingCallback != null) {goingCallback.run();}return;}mLocalDeviceIdleController= LocalServices.getService(DeviceIdleController.LocalService.class);mActivityTaskManager.onSystemReady();// Make sure we have the current profile info, since it is needed for security checks.mUserController.onSystemReady();mAppOpsService.systemReady();mSystemReady = true;}try {sTheRealBuildSerial = IDeviceIdentifiersPolicyService.Stub.asInterface(ServiceManager.getService(Context.DEVICE_IDENTIFIERS_SERVICE)).getSerial();} catch (RemoteException e) {}ArrayList<ProcessRecord> procsToKill = null;synchronized(mPidsSelfLocked) {for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {ProcessRecord proc = mPidsSelfLocked.valueAt(i);if (!isAllowedWhileBooting(proc.info)){if (procsToKill == null) {procsToKill = new ArrayList<ProcessRecord>();}procsToKill.add(proc);}}}synchronized(this) {if (procsToKill != null) {for (int i=procsToKill.size()-1; i>=0; i--) {ProcessRecord proc = procsToKill.get(i);Slog.i(TAG, "Removing system update proc: " + proc);mProcessList.removeProcessLocked(proc, true, false, "system update done");}}// Now that we have cleaned up any update processes, we// are ready to start launching real processes and know that// we won't trample on them any more.mProcessesReady = true;}Slog.i(TAG, "System now ready");EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, SystemClock.uptimeMillis());mAtmInternal.updateTopComponentForFactoryTest();mAtmInternal.getLaunchObserverRegistry().registerLaunchObserver(mActivityLaunchObserver);watchDeviceProvisioning(mContext);retrieveSettings();mUgmInternal.onSystemReady();final PowerManagerInternal pmi = LocalServices.getService(PowerManagerInternal.class);if (pmi != null) {pmi.registerLowPowerModeObserver(ServiceType.FORCE_BACKGROUND_CHECK,state -> updateForceBackgroundCheck(state.batterySaverEnabled));updateForceBackgroundCheck(pmi.getLowPowerState(ServiceType.FORCE_BACKGROUND_CHECK).batterySaverEnabled);} else {Slog.wtf(TAG, "PowerManagerInternal not found.");}if (goingCallback != null) goingCallback.run();// Check the current user here as a user can be started inside goingCallback.run() from// other system services.final int currentUserId = mUserController.getCurrentUserId();Slog.i(TAG, "Current user:" + currentUserId);if (currentUserId != UserHandle.USER_SYSTEM && !mUserController.isSystemUserStarted()) {// User other than system user has started. Make sure that system user is already// started before switching user.throw new RuntimeException("System user not started while current user is:"+ currentUserId);}traceLog.traceBegin("ActivityManagerStartApps");mBatteryStatsService.noteEvent(BatteryStats.HistoryItem.EVENT_USER_RUNNING_START,Integer.toString(currentUserId), currentUserId);mBatteryStatsService.noteEvent(BatteryStats.HistoryItem.EVENT_USER_FOREGROUND_START,Integer.toString(currentUserId), currentUserId);// On Automotive, at this point the system user has already been started and unlocked,// and some of the tasks we do here have already been done. So skip those in that case.// TODO(b/132262830): this workdound shouldn't be necessary once we move the// headless-user start logic to UserManager-landfinal boolean bootingSystemUser = currentUserId == UserHandle.USER_SYSTEM;if (bootingSystemUser) {mSystemServiceManager.startUser(currentUserId);}synchronized (this) {// Only start up encryption-aware persistent apps; once user is// unlocked we'll come back around and start unaware appsstartPersistentApps(PackageManager.MATCH_DIRECT_BOOT_AWARE);// Start up initial activity.mBooting = true;// Enable home activity for system user, so that the system can always boot. We don't// do this when the system user is not setup since the setup wizard should be the one// to handle home activity in this case.if (UserManager.isSplitSystemUser() &&Settings.Secure.getInt(mContext.getContentResolver(),Settings.Secure.USER_SETUP_COMPLETE, 0) != 0) {ComponentName cName = new ComponentName(mContext, SystemUserHomeActivity.class);try {AppGlobals.getPackageManager().setComponentEnabledSetting(cName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0,UserHandle.USER_SYSTEM);} catch (RemoteException e) {throw e.rethrowAsRuntimeException();}}if (bootingSystemUser) {①mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");}mAtmInternal.showSystemReadyErrorDialogsIfNeeded();if (bootingSystemUser) {final int callingUid = Binder.getCallingUid();final int callingPid = Binder.getCallingPid();long ident = Binder.clearCallingIdentity();try {Intent intent = new Intent(Intent.ACTION_USER_STARTED);intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY| Intent.FLAG_RECEIVER_FOREGROUND);intent.putExtra(Intent.EXTRA_USER_HANDLE, currentUserId);broadcastIntentLocked(null, null, intent,null, null, 0, null, null, null, OP_NONE,null, false, false, MY_PID, SYSTEM_UID, callingUid, callingPid,currentUserId);intent = new Intent(Intent.ACTION_USER_STARTING);intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);intent.putExtra(Intent.EXTRA_USER_HANDLE, currentUserId);broadcastIntentLocked(null, null, intent,null, new IIntentReceiver.Stub() {@Overridepublic void performReceive(Intent intent, int resultCode, String data,Bundle extras, boolean ordered, boolean sticky, int sendingUser)throws RemoteException {}}, 0, null, null,new String[] {INTERACT_ACROSS_USERS}, OP_NONE,null, true, false, MY_PID, SYSTEM_UID, callingUid, callingPid,UserHandle.USER_ALL);} catch (Throwable t) {Slog.wtf(TAG, "Failed sending first user broadcasts", t);} finally {Binder.restoreCallingIdentity(ident);}} else {Slog.i(TAG, "Not sending multi-user broadcasts for non-system user "+ currentUserId);}mAtmInternal.resumeTopActivities(false /* scheduleIdle */);if (bootingSystemUser) {mUserController.sendUserSwitchBroadcasts(-1, currentUserId);}BinderInternal.nSetBinderProxyCountWatermarks(BINDER_PROXY_HIGH_WATERMARK,BINDER_PROXY_LOW_WATERMARK);BinderInternal.nSetBinderProxyCountEnabled(true);BinderInternal.setBinderProxyCountCallback(new BinderInternal.BinderProxyLimitListener() {@Overridepublic void onLimitReached(int uid) {Slog.wtf(TAG, "Uid " + uid + " sent too many Binders to uid "+ Process.myUid());BinderProxy.dumpProxyDebugInfo();if (uid == Process.SYSTEM_UID) {Slog.i(TAG, "Skipping kill (uid is SYSTEM)");} else {killUid(UserHandle.getAppId(uid), UserHandle.getUserId(uid),"Too many Binders sent to SYSTEM");}}}, mHandler);traceLog.traceEnd(); // ActivityManagerStartAppstraceLog.traceEnd(); // PhaseActivityManagerReady}}

mAtmInternal.startHomeOnAllDisplays(currentUserId, “systemReady”);
mAtmInternal的实现类是ActivityTaskManagerService的内部类LocalService

三、ActivityTaskManagerService

frameworks\\base\\services\\core\\java\\com\\android\\server\\wm\\ActivityTaskManagerService.java

3.1、startHomeOnAllDisplays

 @Overridepublic boolean startHomeOnAllDisplays(int userId, String reason) {synchronized (mGlobalLock) {return mRootActivityContainer.startHomeOnAllDisplays(userId, reason);}}

四、RootActivityContainer

frameworks\\base\\services\\core\\java\\com\\android\\server\\wm\\RootActivityContainer.java

4.1、startHomeOnAllDisplays

boolean startHomeOnAllDisplays(int userId, String reason) {boolean homeStarted = false;for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {final int displayId = mActivityDisplays.get(i).mDisplayId;homeStarted |= startHomeOnDisplay(userId, reason, displayId);}return homeStarted;}boolean startHomeOnDisplay(int userId, String reason, int displayId) {return startHomeOnDisplay(userId, reason, displayId, false /* allowInstrumenting */,false /* fromHomeKey */);}boolean startHomeOnDisplay(int userId, String reason, int displayId, boolean allowInstrumenting,boolean fromHomeKey) {// Fallback to top focused display if the displayId is invalid.if (displayId == INVALID_DISPLAY) {displayId = getTopDisplayFocusedStack().mDisplayId;}Intent homeIntent = null;ActivityInfo aInfo = null;if (displayId == DEFAULT_DISPLAY) {homeIntent = mService.getHomeIntent();aInfo = resolveHomeActivity(userId, homeIntent);} else if (shouldPlaceSecondaryHomeOnDisplay(displayId)) {Pair<ActivityInfo, Intent> info = resolveSecondaryHomeActivity(userId, displayId);aInfo = info.first;homeIntent = info.second;}if (aInfo == null || homeIntent == null) {return false;}if (!canStartHomeOnDisplay(aInfo, displayId, 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);}// 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) + ":" + displayId;mService.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason,displayId);return true;}

最后调用的是ActivityStartController的startHomeActivity方法。

五、ActivityStartController

frameworks\\base\\services\\core\\java\\com\\android\\server\\wm\\ActivityStartController.java

5.1、startHomeActivity

void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason, int displayId) {final ActivityOptions options = ActivityOptions.makeBasic();options.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);if (!ActivityRecord.isResolverActivity(aInfo.name)) {// The resolver activity shouldn't be put in home stack because when the foreground is// standard type activity, the resolver activity should be put on the top of current// foreground instead of bring home stack to front.options.setLaunchActivityType(ACTIVITY_TYPE_HOME);}options.setLaunchDisplayId(displayId);mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason).setOutActivity(tmpOutRecord).setCallingUid(0).setActivityInfo(aInfo).setActivityOptions(options.toBundle()).execute();mLastHomeActivityStartRecord = tmpOutRecord[0];final ActivityDisplay display =mService.mRootActivityContainer.getActivityDisplay(displayId);final ActivityStack homeStack = display != null ? display.getHomeStack() : null;if (homeStack != null && homeStack.mInResumeTopActivity) {// If we are in resume section already, home activity will be initialized, but not// resumed (to avoid recursive resume) and will stay that way until something pokes it// again. We need to schedule another resume.mSupervisor.scheduleResumeTopActivities();}}

六、ActivityStarter

frameworks\\base\\services\\core\\java\\com\\android\\server\\wm\\ActivityStarter.java

6.1、execute

 /*** Starts an activity based on the request parameters provided earlier.* @return The starter result.*/int execute() {try {// TODO(b/64750076): Look into passing request directly to these methods to allow// for transactional diffs and preprocessing.if (mRequest.mayWait) {return startActivityMayWait(mRequest.caller, mRequest.callingUid,mRequest.callingPackage, mRequest.realCallingPid, mRequest.realCallingUid,mRequest.intent, mRequest.resolvedType,mRequest.voiceSession, mRequest.voiceInteractor, mRequest.resultTo,mRequest.resultWho, mRequest.requestCode, mRequest.startFlags,mRequest.profilerInfo, mRequest.waitResult, mRequest.globalConfig,mRequest.activityOptions, mRequest.ignoreTargetSecurity, mRequest.userId,mRequest.inTask, mRequest.reason,mRequest.allowPendingRemoteAnimationRegistryLookup,mRequest.originatingPendingIntent, mRequest.allowBackgroundActivityStart);} else {return startActivity(mRequest.caller, mRequest.intent, mRequest.ephemeralIntent,mRequest.resolvedType, mRequest.activityInfo, mRequest.resolveInfo,mRequest.voiceSession, mRequest.voiceInteractor, mRequest.resultTo,mRequest.resultWho, mRequest.requestCode, mRequest.callingPid,mRequest.callingUid, mRequest.callingPackage, mRequest.realCallingPid,mRequest.realCallingUid, mRequest.startFlags, mRequest.activityOptions,mRequest.ignoreTargetSecurity, mRequest.componentSpecified,mRequest.outActivity, mRequest.inTask, mRequest.reason,mRequest.allowPendingRemoteAnimationRegistryLookup,mRequest.originatingPendingIntent, mRequest.allowBackgroundActivityStart);}} finally {onExecutionComplete();}}

6.2、startActivity

 private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,String callingPackage, int realCallingPid, int realCallingUid, int startFlags,SafeActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,ActivityRecord[] outActivity, TaskRecord inTask, String reason,boolean allowPendingRemoteAnimationRegistryLookup,PendingIntentRecord originatingPendingIntent, boolean allowBackgroundActivityStart) {if (TextUtils.isEmpty(reason)) {throw new IllegalArgumentException("Need to specify a reason.");}mLastStartReason = reason;mLastStartActivityTimeMs = System.currentTimeMillis();mLastStartActivityRecord[0] = null;mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,inTask, allowPendingRemoteAnimationRegistryLookup, originatingPendingIntent,allowBackgroundActivityStart);if (outActivity != null) {// mLastStartActivityRecord[0] is set in the call to startActivity above.outActivity[0] = mLastStartActivityRecord[0];}return getExternalResult(mLastStartActivityResult);}private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,String callingPackage, int realCallingPid, int realCallingUid, int startFlags,SafeActivityOptions options,boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,TaskRecord inTask, boolean allowPendingRemoteAnimationRegistryLookup,PendingIntentRecord originatingPendingIntent, boolean allowBackgroundActivityStart) {mSupervisor.getActivityMetricsLogger().notifyActivityLaunching(intent);int err = ActivityManager.START_SUCCESS;// Pull the optional Ephemeral Installer-only bundle out of the options early.final Bundle verificationBundle= options != null ? options.popAppVerificationBundle() : null;WindowProcessController callerApp = null;if (caller != null) {callerApp = mService.getProcessController(caller);if (callerApp != null) {callingPid = callerApp.getPid();callingUid = callerApp.mInfo.uid;} else {Slog.w(TAG, "Unable to find app for caller " + caller+ " (pid=" + callingPid + ") when starting: "+ intent.toString());err = ActivityManager.START_PERMISSION_DENIED;}}final int userId = aInfo != null && aInfo.applicationInfo != null? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;if (err == ActivityManager.START_SUCCESS) {Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)+ "} from uid " + callingUid);}ActivityRecord sourceRecord = null;ActivityRecord resultRecord = null;if (resultTo != null) {sourceRecord = mRootActivityContainer.isInAnyStack(resultTo);if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,"Will send result to " + resultTo + " " + sourceRecord);if (sourceRecord != null) {if (requestCode >= 0 && !sourceRecord.finishing) {resultRecord = sourceRecord;}}}final int launchFlags = intent.getFlags();if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {// Transfer the result target from the source activity to the new// one being started, including any failures.if (requestCode >= 0) {SafeActivityOptions.abort(options);return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;}resultRecord = sourceRecord.resultTo;if (resultRecord != null && !resultRecord.isInStackLocked()) {resultRecord = null;}resultWho = sourceRecord.resultWho;requestCode = sourceRecord.requestCode;sourceRecord.resultTo = null;if (resultRecord != null) {resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);}if (sourceRecord.launchedFromUid == callingUid) {// The new activity is being launched from the same uid as the previous// activity in the flow, and asking to forward its result back to the// previous.  In this case the activity is serving as a trampoline between// the two, so we also want to update its launchedFromPackage to be the// same as the previous activity.  Note that this is safe, since we know// these two packages come from the same uid; the caller could just as// well have supplied that same package name itself.  This specifially// deals with the case of an intent picker/chooser being launched in the app// flow to redirect to an activity picked by the user, where we want the final// activity to consider it to have been launched by the previous app activity.callingPackage = sourceRecord.launchedFromPackage;}}if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {// We couldn't find a class that can handle the given Intent.// That's the end of that!err = ActivityManager.START_INTENT_NOT_RESOLVED;}if (err == ActivityManager.START_SUCCESS && aInfo == null) {// We couldn't find the specific class specified in the Intent.// Also the end of the line.err = ActivityManager.START_CLASS_NOT_FOUND;}if (err == ActivityManager.START_SUCCESS && sourceRecord != null&& sourceRecord.getTaskRecord().voiceSession != null) {// If this activity is being launched as part of a voice session, we need// to ensure that it is safe to do so.  If the upcoming activity will also// be part of the voice session, we can only launch it if it has explicitly// said it supports the VOICE category, or it is a part of the calling app.if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0&& sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {try {intent.addCategory(Intent.CATEGORY_VOICE);if (!mService.getPackageManager().activitySupportsIntent(intent.getComponent(), intent, resolvedType)) {Slog.w(TAG,"Activity being started in current voice task does not support voice: "+ intent);err = ActivityManager.START_NOT_VOICE_COMPATIBLE;}} catch (RemoteException e) {Slog.w(TAG, "Failure checking voice capabilities", e);err = ActivityManager.START_NOT_VOICE_COMPATIBLE;}}}if (err == ActivityManager.START_SUCCESS && voiceSession != null) {// If the caller is starting a new voice session, just make sure the target// is actually allowing it to run this way.try {if (!mService.getPackageManager().activitySupportsIntent(intent.getComponent(),intent, resolvedType)) {Slog.w(TAG,"Activity being started in new voice task does not support: "+ intent);err = ActivityManager.START_NOT_VOICE_COMPATIBLE;}} catch (RemoteException e) {Slog.w(TAG, "Failure checking voice capabilities", e);err = ActivityManager.START_NOT_VOICE_COMPATIBLE;}}final ActivityStack resultStack = resultRecord == null? null : resultRecord.getActivityStack();if (err != START_SUCCESS) {if (resultRecord != null) {resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode, RESULT_CANCELED, null);}SafeActivityOptions.abort(options);return err;}boolean abort = !mSupervisor.checkStartAnyActivityPermission(intent, aInfo, resultWho,requestCode, callingPid, callingUid, callingPackage, ignoreTargetSecurity,inTask != null, callerApp, resultRecord, resultStack);abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,callingPid, resolvedType, aInfo.applicationInfo);abort |= !mService.getPermissionPolicyInternal().checkStartActivity(intent, callingUid,callingPackage);boolean restrictedBgActivity = false;if (!abort) {try {Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,"shouldAbortBackgroundActivityStart");restrictedBgActivity = shouldAbortBackgroundActivityStart(callingUid,callingPid, callingPackage, realCallingUid, realCallingPid, callerApp,originatingPendingIntent, allowBackgroundActivityStart, intent);} finally {Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);}}// Merge the two options bundles, while realCallerOptions takes precedence.ActivityOptions checkedOptions = options != null? options.getOptions(intent, aInfo, callerApp, mSupervisor) : null;if (allowPendingRemoteAnimationRegistryLookup) {checkedOptions = mService.getActivityStartController().getPendingRemoteAnimationRegistry().overrideOptionsIfNeeded(callingPackage, checkedOptions);}if (mService.mController != null) {try {// The Intent we give to the watcher has the extra data// stripped off, since it can contain private information.Intent watchIntent = intent.cloneFilter();abort |= !mService.mController.activityStarting(watchIntent,aInfo.applicationInfo.packageName);} catch (RemoteException e) {mService.mController = null;}}mInterceptor.setStates(userId, realCallingPid, realCallingUid, startFlags, callingPackage);if (mInterceptor.intercept(intent, rInfo, aInfo, resolvedType, inTask, callingPid,callingUid, checkedOptions)) {// activity start was intercepted, e.g. because the target user is currently in quiet// mode (turn off work) or the target application is suspendedintent = mInterceptor.mIntent;rInfo = mInterceptor.mRInfo;aInfo = mInterceptor.mAInfo;resolvedType = mInterceptor.mResolvedType;inTask = mInterceptor.mInTask;callingPid = mInterceptor.mCallingPid;callingUid = mInterceptor.mCallingUid;checkedOptions = mInterceptor.mActivityOptions;}if (abort) {if (resultRecord != null) {resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,RESULT_CANCELED, null);}// We pretend to the caller that it was really started, but// they will just get a cancel result.ActivityOptions.abort(checkedOptions);return START_ABORTED;}// If permissions need a review before any of the app components can run, we// launch the review activity and pass a pending intent to start the activity// we are to launching now after the review is completed.if (aInfo != null) {if (mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(aInfo.packageName, userId)) {IIntentSender target = mService.getIntentSenderLocked(ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,callingUid, userId, null, null, 0, new Intent[]{intent},new String[]{resolvedType}, PendingIntent.FLAG_CANCEL_CURRENT| PendingIntent.FLAG_ONE_SHOT, null);Intent newIntent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);int flags = intent.getFlags();flags |= Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;/** Prevent reuse of review activity: Each app needs their own review activity. By* default activities launched with NEW_TASK or NEW_DOCUMENT try to reuse activities* with the same launch parameters (extras are ignored). Hence to avoid possible* reuse force a new activity via the MULTIPLE_TASK flag.** Activities that are not launched with NEW_TASK or NEW_DOCUMENT are not re-used,* hence no need to add the flag in this case.*/if ((flags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NEW_DOCUMENT)) != 0) {flags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;}newIntent.setFlags(flags);newIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);newIntent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));if (resultRecord != null) {newIntent.putExtra(Intent.EXTRA_RESULT_NEEDED, true);}intent = newIntent;resolvedType = null;callingUid = realCallingUid;callingPid = realCallingPid;rInfo = mSupervisor.resolveIntent(intent, resolvedType, userId, 0,computeResolveFilterUid(callingUid, realCallingUid, mRequest.filterCallingUid));aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags,null /*profilerInfo*/);if (DEBUG_PERMISSIONS_REVIEW) {final ActivityStack focusedStack =mRootActivityContainer.getTopDisplayFocusedStack();Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true,true, false) + "} from uid " + callingUid + " on display "+ (focusedStack == null ? DEFAULT_DISPLAY : focusedStack.mDisplayId));}}}// If we have an ephemeral app, abort the process of launching the resolved intent.// Instead, launch the ephemeral installer. Once the installer is finished, it// starts either the intent we resolved here [on install error] or the ephemeral// app [on install success].if (rInfo != null && rInfo.auxiliaryInfo != null) {intent = createLaunchIntent(rInfo.auxiliaryInfo, ephemeralIntent,callingPackage, verificationBundle, resolvedType, userId);resolvedType = null;callingUid = realCallingUid;callingPid = realCallingPid;aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);}ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),resultRecord, resultWho, requestCode, componentSpecified, voiceSession != null,mSupervisor, checkedOptions, sourceRecord);if (outActivity != null) {outActivity[0] = r;}if (r.appTimeTracker == null && sourceRecord != null) {// If the caller didn't specify an explicit time tracker, we want to continue// tracking under any it has.r.appTimeTracker = sourceRecord.appTimeTracker;}final ActivityStack stack = mRootActivityContainer.getTopDisplayFocusedStack();// If we are starting an activity that is not from the same uid as the currently resumed// one, check whether app switches are allowed.if (voiceSession == null && (stack.getResumedActivity() == null|| stack.getResumedActivity().info.applicationInfo.uid != realCallingUid)) {if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,realCallingPid, realCallingUid, "Activity start")) {if (!(restrictedBgActivity && handleBackgroundActivityAbort(r))) {mController.addPendingActivityLaunch(new PendingActivityLaunch(r,sourceRecord, startFlags, stack, callerApp));}ActivityOptions.abort(checkedOptions);return ActivityManager.START_SWITCHES_CANCELED;}}mService.onStartActivitySetDidAppSwitch();mController.doPendingActivityLaunches(false);final int res = startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags,true /* doResume */, checkedOptions, inTask, outActivity, restrictedBgActivity);mSupervisor.getActivityMetricsLogger().notifyActivityLaunched(res, outActivity[0]);return res;}private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,ActivityRecord[] outActivity, boolean restrictedBgActivity) {int result = START_CANCELED;final ActivityStack startedActivityStack;try {mService.mWindowManager.deferSurfaceLayout();①result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,startFlags, doResume, options, inTask, outActivity, restrictedBgActivity);} finally {final ActivityStack currentStack = r.getActivityStack();startedActivityStack = currentStack != null ? currentStack : mTargetStack;if (ActivityManager.isStartResultSuccessful(result)) {if (startedActivityStack != null) {// If there is no state change (e.g. a resumed activity is reparented to// top of another display) to trigger a visibility/configuration checking,// we have to update the configuration for changing to different display.final ActivityRecord currentTop =startedActivityStack.topRunningActivityLocked();if (currentTop != null && currentTop.shouldUpdateConfigForDisplayChanged()) {mRootActivityContainer.ensureVisibilityAndConfig(currentTop, currentTop.getDisplayId(),true /* markFrozenIfConfigChanged */, false /* deferResume */);}}} else {// If we are not able to proceed, disassociate the activity from the task.// Leaving an activity in an incomplete state can lead to issues, such as// performing operations without a window container.final ActivityStack stack = mStartActivity.getActivityStack();if (stack != null) {stack.finishActivityLocked(mStartActivity, RESULT_CANCELED,null /* intentResultData */, "startActivity", true /* oomAdj */);}// Stack should also be detached from display and be removed if it's empty.if (startedActivityStack != null && startedActivityStack.isAttached()&& startedActivityStack.numActivities() == 0&& !startedActivityStack.isActivityTypeHome()) {startedActivityStack.remove();}}mService.mWindowManager.continueSurfaceLayout();}postStartActivityProcessing(r, result, startedActivityStack);return result;}
  • startActivity最终调用的是startActivityUnchecked方法

6.3、startActivityUnchecked

 // Note: This method should only be called from {@link startActivity}.private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,ActivityRecord[] outActivity, boolean restrictedBgActivity) {setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,voiceInteractor, restrictedBgActivity);final int preferredWindowingMode = mLaunchParams.mWindowingMode;computeLaunchingTaskFlags();computeSourceStack();mIntent.setFlags(mLaunchFlags);ActivityRecord reusedActivity = getReusableIntentActivity();mSupervisor.getLaunchParamsController().calculate(reusedActivity != null ? reusedActivity.getTaskRecord() : mInTask,r.info.windowLayout, r, sourceRecord, options, PHASE_BOUNDS, mLaunchParams);mPreferredDisplayId =mLaunchParams.hasPreferredDisplay() ? mLaunchParams.mPreferredDisplayId: DEFAULT_DISPLAY;// If requested, freeze the task listif (mOptions != null && mOptions.freezeRecentTasksReordering()&& mSupervisor.mRecentTasks.isCallerRecents(r.launchedFromUid)&& !mSupervisor.mRecentTasks.isFreezeTaskListReorderingSet()) {mFrozeTaskList = true;mSupervisor.mRecentTasks.setFreezeTaskListReordering();}// Do not start home activity if it cannot be launched on preferred display. We are not// doing this in ActivityStackSupervisor#canPlaceEntityOnDisplay because it might// fallback to launch on other displays.if (r.isActivityTypeHome() && !mRootActivityContainer.canStartHomeOnDisplay(r.info,mPreferredDisplayId, true /* allowInstrumenting */)) {Slog.w(TAG, "Cannot launch home on display " + mPreferredDisplayId);return START_CANCELED;}if (reusedActivity != null) {// When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused but// still needs to be a lock task mode violation since the task gets cleared out and// the device would otherwise leave the locked task.if (mService.getLockTaskController().isLockTaskModeViolation(reusedActivity.getTaskRecord(),(mLaunchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))== (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");return START_RETURN_LOCK_TASK_MODE_VIOLATION;}// True if we are clearing top and resetting of a standard (default) launch mode// ({@code LAUNCH_MULTIPLE}) activity. The existing activity will be finished.final boolean clearTopAndResetStandardLaunchMode =(mLaunchFlags & (FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_RESET_TASK_IF_NEEDED))== (FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)&& mLaunchMode == LAUNCH_MULTIPLE;// If mStartActivity does not have a task associated with it, associate it with the// reused activity's task. Do not do so if we're clearing top and resetting for a// standard launchMode activity.if (mStartActivity.getTaskRecord() == null && !clearTopAndResetStandardLaunchMode) {mStartActivity.setTask(reusedActivity.getTaskRecord());}if (reusedActivity.getTaskRecord().intent == null) {// This task was started because of movement of the activity based on affinity...// Now that we are actually launching it, we can assign the base intent.reusedActivity.getTaskRecord().setIntent(mStartActivity);} else {final boolean taskOnHome =(mStartActivity.intent.getFlags() & FLAG_ACTIVITY_TASK_ON_HOME) != 0;if (taskOnHome) {reusedActivity.getTaskRecord().intent.addFlags(FLAG_ACTIVITY_TASK_ON_HOME);} else {reusedActivity.getTaskRecord().intent.removeFlags(FLAG_ACTIVITY_TASK_ON_HOME);}}// This code path leads to delivering a new intent, we want to make sure we schedule it// as the first operation, in case the activity will be resumed as a result of later// operations.if ((mLaunchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0|| isDocumentLaunchesIntoExisting(mLaunchFlags)|| isLaunchModeOneOf(LAUNCH_SINGLE_INSTANCE, LAUNCH_SINGLE_TASK)) {final TaskRecord task = reusedActivity.getTaskRecord();// In this situation we want to remove all activities from the task up to the one// being started. In most cases this means we are resetting the task to its initial// state.final ActivityRecord top = task.performClearTaskForReuseLocked(mStartActivity,mLaunchFlags);// The above code can remove {@code reusedActivity} from the task, leading to the// the {@code ActivityRecord} removing its reference to the {@code TaskRecord}. The// task reference is needed in the call below to// {@link setTargetStackAndMoveToFrontIfNeeded}.if (reusedActivity.getTaskRecord() == null) {reusedActivity.setTask(task);}if (top != null) {if (top.frontOfTask) {// Activity aliases may mean we use different intents for the top activity,// so make sure the task now has the identity of the new intent.top.getTaskRecord().setIntent(mStartActivity);}deliverNewIntent(top);}}mRootActivityContainer.sendPowerHintForLaunchStartIfNeeded(false /* forceSend */, reusedActivity);reusedActivity = setTargetStackAndMoveToFrontIfNeeded(reusedActivity);final ActivityRecord outResult =outActivity != null && outActivity.length > 0 ? outActivity[0] : null;// When there is a reused activity and the current result is a trampoline activity,// set the reused activity as the result.if (outResult != null && (outResult.finishing || outResult.noDisplay)) {outActivity[0] = reusedActivity;}if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {// We don't need to start a new activity, and the client said not to do anything// if that is the case, so this is it!  And for paranoia, make sure we have// correctly resumed the top activity.resumeTargetStackIfNeeded();return START_RETURN_INTENT_TO_CALLER;}if (reusedActivity != null) {setTaskFromIntentActivity(reusedActivity);if (!mAddingToTask && mReuseTask == null) {// We didn't do anything...  but it was needed (a.k.a., client don't use that// intent!)  And for paranoia, make sure we have correctly resumed the top activity.resumeTargetStackIfNeeded();if (outActivity != null && outActivity.length > 0) {// The reusedActivity could be finishing, for example of starting an// activity with FLAG_ACTIVITY_CLEAR_TOP flag. In that case, return the// top running activity in the task instead.outActivity[0] = reusedActivity.finishing? reusedActivity.getTaskRecord().getTopActivity() : reusedActivity;}return mMovedToFront ? START_TASK_TO_FRONT : START_DELIVERED_TO_TOP;}}}if (mStartActivity.packageName == null) {final ActivityStack sourceStack = mStartActivity.resultTo != null? mStartActivity.resultTo.getActivityStack() : null;if (sourceStack != null) {sourceStack.sendActivityResultLocked(-1 /* callingUid */, mStartActivity.resultTo,mStartActivity.resultWho, mStartActivity.requestCode, RESULT_CANCELED,null /* data */);}ActivityOptions.abort(mOptions);return START_CLASS_NOT_FOUND;}// If the activity being launched is the same as the one currently at the top, then// we need to check if it should only be launched once.final ActivityStack topStack = mRootActivityContainer.getTopDisplayFocusedStack();final ActivityRecord topFocused = topStack.getTopActivity();final ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(mNotTop);final boolean dontStart = top != null && mStartActivity.resultTo == null&& top.mActivityComponent.equals(mStartActivity.mActivityComponent)&& top.mUserId == mStartActivity.mUserId&& top.attachedToProcess()&& ((mLaunchFlags & FLAG_ACTIVITY_SINGLE_TOP) != 0|| isLaunchModeOneOf(LAUNCH_SINGLE_TOP, LAUNCH_SINGLE_TASK))// This allows home activity to automatically launch on secondary display when// display added, if home was the top activity on default display, instead of// sending new intent to the home activity on default display.&& (!top.isActivityTypeHome() || top.getDisplayId() == mPreferredDisplayId);if (dontStart) {// For paranoia, make sure we have correctly resumed the top activity.topStack.mLastPausedActivity = null;if (mDoResume) {mRootActivityContainer.resumeFocusedStacksTopActivities();}ActivityOptions.abort(mOptions);if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {// We don't need to start a new activity, and the client said not to do// anything if that is the case, so this is it!return START_RETURN_INTENT_TO_CALLER;}deliverNewIntent(top);// Don't use mStartActivity.task to show the toast. We're not starting a new activity// but reusing 'top'. Fields in mStartActivity may not be fully initialized.mSupervisor.handleNonResizableTaskIfNeeded(top.getTaskRecord(), preferredWindowingMode,mPreferredDisplayId, topStack);return START_DELIVERED_TO_TOP;}boolean newTask = false;final TaskRecord taskToAffiliate = (mLaunchTaskBehind && mSourceRecord != null)? mSourceRecord.getTaskRecord() : null;// Should this be considered a new task?int result = START_SUCCESS;if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask&& (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {newTask = true;result = setTaskFromReuseOrCreateNewTask(taskToAffiliate);} else if (mSourceRecord != null) {result = setTaskFromSourceRecord();} else if (mInTask != null) {result = setTaskFromInTask();} else {// This not being started from an existing activity, and not part of a new task...// just put it in the top task, though these days this case should never happen.result = setTaskToCurrentTopOrCreateNewTask();}if (result != START_SUCCESS) {return result;}mService.mUgmInternal.grantUriPermissionFromIntent(mCallingUid, mStartActivity.packageName,mIntent, mStartActivity.getUriPermissionsLocked(), mStartActivity.mUserId);mService.getPackageManagerInternalLocked().grantEphemeralAccess(mStartActivity.mUserId, mIntent, UserHandle.getAppId(mStartActivity.appInfo.uid),UserHandle.getAppId(mCallingUid));if (newTask) {EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, mStartActivity.mUserId,mStartActivity.getTaskRecord().taskId);}ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, mStartActivity, mStartActivity.getTaskRecord());mTargetStack.mLastPausedActivity = null;mRootActivityContainer.sendPowerHintForLaunchStartIfNeeded(false /* forceSend */, mStartActivity);mTargetStack.startActivityLocked(mStartActivity, topFocused, newTask, mKeepCurTransition,mOptions);if (mDoResume) {final ActivityRecord topTaskActivity =mStartActivity.getTaskRecord().topRunningActivityLocked();if (!mTargetStack.isFocusable()|| (topTaskActivity != null && topTaskActivity.mTaskOverlay&& mStartActivity != topTaskActivity)) {// If the activity is not focusable, we can't resume it, but still would like to// make sure it becomes visible as it starts (this will also trigger entry// animation). An example of this are PIP activities.// Also, we don't want to resume activities in a task that currently has an overlay// as the starting activity just needs to be in the visible paused state until the// over is removed.mTargetStack.ensureActivitiesVisibleLocked(mStartActivity, 0, !PRESERVE_WINDOWS);// Go ahead and tell window manager to execute app transition for this activity// since the app transition will not be triggered through the resume channel.mTargetStack.getDisplay().mDisplayContent.executeAppTransition();} else {// If the target stack was not previously focusable (previous top running activity// on that stack was not visible) then any prior calls to move the stack to the// will not update the focused stack.  If starting the new activity now allows the// task stack to be focusable, then ensure that we now update the focused stack// accordingly.if (mTargetStack.isFocusable()&& !mRootActivityContainer.isTopDisplayFocusedStack(mTargetStack)) {mTargetStack.moveToFront("startActivityUnchecked");}①mRootActivityContainer.resumeFocusedStacksTopActivities(mTargetStack, mStartActivity, mOptions);}} else if (mStartActivity != null) {mSupervisor.mRecentTasks.add(mStartActivity.getTaskRecord());}mRootActivityContainer.updateUserStack(mStartActivity.mUserId, mTargetStack);mSupervisor.handleNonResizableTaskIfNeeded(mStartActivity.getTaskRecord(),preferredWindowingMode, mPreferredDisplayId, mTargetStack);return START_SUCCESS;}

最终会走到①这里,调用RootActivityContainer的resumeFocusedStacksTopActivities

七、RootActivityContainer

\\frameworks\\base\\services\\core\\java\\com\\android\\server\\wm\\RootActivityContainer.java

7.1、resumeFocusedStacksTopActivities

 boolean resumeFocusedStacksTopActivities(ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {if (!mStackSupervisor.readyToResume()) {return false;}boolean result = false;if (targetStack != null && (targetStack.isTopStackOnDisplay()|| getTopDisplayFocusedStack() == targetStack)) {result = targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);}for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {boolean resumedOnDisplay = false;final ActivityDisplay display = mActivityDisplays.get(displayNdx);for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {final ActivityStack stack = display.getChildAt(stackNdx);final ActivityRecord topRunningActivity = stack.topRunningActivityLocked();if (!stack.isFocusableAndVisible() || topRunningActivity == null) {continue;}if (stack == targetStack) {// Simply update the result for targetStack because the targetStack had// already resumed in above. We don't want to resume it again, especially in// some cases, it would cause a second launch failure if app process was dead.resumedOnDisplay |= result;continue;}if (display.isTopStack(stack) && topRunningActivity.isState(RESUMED)) {// Kick off any lingering app transitions form the MoveTaskToFront operation,// but only consider the top task and stack on that display.stack.executeAppTransition(targetOptions);} else {resumedOnDisplay |= topRunningActivity.makeActiveIfNeeded(target);}}if (!resumedOnDisplay) {// In cases when there are no valid activities (e.g. device just booted or launcher// crashed) it's possible that nothing was resumed on a display. Requesting resume// of top activity in focused stack explicitly will make sure that at least home// activity is started and resumed, and no recursion occurs.final ActivityStack focusedStack = display.getFocusedStack();if (focusedStack != null) {focusedStack.resumeTopActivityUncheckedLocked(target, targetOptions);}}}return result;}

八、ActivityStack

frameworks\\base\\services\\core\\java\\com\\android\\server\\wm\\ActivityStack.java

8.1、resumeTopActivityUncheckedLocked

@GuardedBy("mService")boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {if (mInResumeTopActivity) {// Don't even start recursing.return false;}boolean result = false;try {// Protect against recursion.mInResumeTopActivity = true;result = resumeTopActivityInnerLocked(prev, options);// When resuming the top activity, it may be necessary to pause the top activity (for// example, returning to the lock screen. We suppress the normal pause logic in// {@link #resumeTopActivityUncheckedLocked}, since the top activity is resumed at the// end. We call the {@link ActivityStackSupervisor#checkReadyForSleepLocked} again here// to ensure any necessary pause logic occurs. In the case where the Activity will be// shown regardless of the lock screen, the call to// {@link ActivityStackSupervisor#checkReadyForSleepLocked} is skipped.final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);if (next == null || !next.canTurnScreenOn()) {checkReadyForSleep();}} finally {mInResumeTopActivity = false;}return result;}@GuardedBy("mService")private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {if (!mService.isBooting() && !mService.isBooted()) {// Not ready yet!return false;}// Find the next top-most activity to resume in this stack that is not finishing and is// focusable. If it is not focusable, we will fall into the case below to resume the// top activity in the next focusable task.ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);final boolean hasRunningActivity = next != null;// TODO: Maybe this entire condition can get removed?if (hasRunningActivity && !isAttached()) {return false;}mRootActivityContainer.cancelInitializingActivities();// Remember how we'll process this pause/resume situation, and ensure// that the state is reset however we wind up proceeding.boolean userLeaving = mStackSupervisor.mUserLeaving;mStackSupervisor.mUserLeaving = false;if (!hasRunningActivity) {// There are no activities left in the stack, let's look somewhere else.return resumeNextFocusableActivityWhenStackIsEmpty(prev, options);}next.delayedResume = false;final ActivityDisplay display = getDisplay();// If the top activity is the resumed one, nothing to do.if (mResumedActivity == next && next.isState(RESUMED)&& display.allResumedActivitiesComplete()) {// Make sure we have executed any pending transitions, since there// should be nothing left to do at this point.executeAppTransition(options);if (DEBUG_STATES) Slog.d(TAG_STATES,"resumeTopActivityLocked: Top activity resumed " + next);return false;}if (!next.canResumeByCompat()) {return false;}// If we are sleeping, and there is no resumed activity, and the top// activity is paused, well that is the state we want.if (shouldSleepOrShutDownActivities()&& mLastPausedActivity == next&& mRootActivityContainer.allPausedActivitiesComplete()) {// If the current top activity may be able to occlude keyguard but the occluded state// has not been set, update visibility and check again if we should continue to resume.boolean nothingToResume = true;if (!mService.mShuttingDown) {final boolean canShowWhenLocked = !mTopActivityOccludesKeyguard&& next.canShowWhenLocked();final boolean mayDismissKeyguard = mTopDismissingKeyguardActivity != next&& next.mAppWindowToken != null&& next.mAppWindowToken.containsDismissKeyguardWindow();if (canShowWhenLocked || mayDismissKeyguard) {ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,!PRESERVE_WINDOWS);nothingToResume = shouldSleepActivities();}}if (nothingToResume) {// Make sure we have executed any pending transitions, since there// should be nothing left to do at this point.executeAppTransition(options);if (DEBUG_STATES) Slog.d(TAG_STATES,"resumeTopActivityLocked: Going to sleep and all paused");return false;}}// Make sure that the user who owns this activity is started.  If not,// we will just leave it as is because someone should be bringing// another user's activities to the top of the stack.if (!mService.mAmInternal.hasStartedUserState(next.mUserId)) {Slog.w(TAG, "Skipping resume of top activity " + next+ ": user " + next.mUserId + " is stopped");return false;}// The activity may be waiting for stop, but that is no longer// appropriate for it.mStackSupervisor.mStoppingActivities.remove(next);mStackSupervisor.mGoingToSleepActivities.remove(next);next.sleeping = false;if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);// If we are currently pausing an activity, then don't do anything until that is done.if (!mRootActivityContainer.allPausedActivitiesComplete()) {if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,"resumeTopActivityLocked: Skip resume: some activity pausing.");return false;}mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);boolean lastResumedCanPip = false;ActivityRecord lastResumed = null;final ActivityStack lastFocusedStack = display.getLastFocusedStack();if (lastFocusedStack != null && lastFocusedStack != this) {// So, why aren't we using prev here??? See the param comment on the method. prev doesn't// represent the last resumed activity. However, the last focus stack does if it isn't null.lastResumed = lastFocusedStack.mResumedActivity;if (userLeaving && inMultiWindowMode() && lastFocusedStack.shouldBeVisible(next)) {// The user isn't leaving if this stack is the multi-window mode and the last// focused stack should still be visible.if(DEBUG_USER_LEAVING) Slog.i(TAG_USER_LEAVING, "Overriding userLeaving to false"+ " next=" + next + " lastResumed=" + lastResumed);userLeaving = false;}lastResumedCanPip = lastResumed != null && lastResumed.checkEnterPictureInPictureState("resumeTopActivity", userLeaving /* beforeStopping */);}// If the flag RESUME_WHILE_PAUSING is set, then continue to schedule the previous activity// to be paused, while at the same time resuming the new resume activity only if the// previous activity can't go into Pip since we want to give Pip activities a chance to// enter Pip before resuming the next activity.final boolean resumeWhilePausing = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0&& !lastResumedCanPip;boolean pausing = getDisplay().pauseBackStacks(userLeaving, next, false);if (mResumedActivity != null) {if (DEBUG_STATES) Slog.d(TAG_STATES,"resumeTopActivityLocked: Pausing " + mResumedActivity);pausing |= startPausingLocked(userLeaving, false, next, false);}if (pausing && !resumeWhilePausing) {if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,"resumeTopActivityLocked: Skip resume: need to start pausing");// At this point we want to put the upcoming activity's process// at the top of the LRU list, since we know we will be needing it// very soon and it would be a waste to let it get killed if it// happens to be sitting towards the end.if (next.attachedToProcess()) {next.app.updateProcessInfo(false /* updateServiceConnectionActivities */,true /* activityChange */, false /* updateOomAdj */);}if (lastResumed != null) {lastResumed.setWillCloseOrEnterPip(true);}return true;} else if (mResumedActivity == next && next.isState(RESUMED)&& display.allResumedActivitiesComplete()) {// It is possible for the activity to be resumed when we paused back stacks above if the// next activity doesn't have to wait for pause to complete.// So, nothing else to-do except:// Make sure we have executed any pending transitions, since there// should be nothing left to do at this point.executeAppTransition(options);if (DEBUG_STATES) Slog.d(TAG_STATES,"resumeTopActivityLocked: Top activity resumed (dontWaitForPause) " + next);return true;}// If the most recent activity was noHistory but was only stopped rather// than stopped+finished because the device went to sleep, we need to make// sure to finish it as we're making a new activity topmost.if (shouldSleepActivities() && mLastNoHistoryActivity != null &&!mLastNoHistoryActivity.finishing) {if (DEBUG_STATES) Slog.d(TAG_STATES,"no-history finish of " + mLastNoHistoryActivity + " on new resume");requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,null, "resume-no-history", false);mLastNoHistoryActivity = null;}if (prev != null && prev != next && next.nowVisible) {// The next activity is already visible, so hide the previous// activity's windows right now so we can show the new one ASAP.// We only do this if the previous is finishing, which should mean// it is on top of the one being resumed so hiding it quickly// is good.  Otherwise, we want to do the normal route of allowing// the resumed activity to be shown so we can decide if the// previous should actually be hidden depending on whether the// new one is found to be full-screen or not.if (prev.finishing) {prev.setVisibility(false);if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,"Not waiting for visible to hide: " + prev+ ", nowVisible=" + next.nowVisible);} else {if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,"Previous already visible but still waiting to hide: " + prev+ ", nowVisible=" + next.nowVisible);}}// Launching this app's activity, make sure the app is no longer// considered stopped.try {AppGlobals.getPackageManager().setPackageStoppedState(next.packageName, false, next.mUserId); /* TODO: Verify if correct userid */} catch (RemoteException e1) {} catch (IllegalArgumentException e) {Slog.w(TAG, "Failed trying to unstop package "+ next.packageName + ": " + e);}// We are starting up the next activity, so tell the window manager// that the previous one will be hidden soon.  This way it can know// to ignore it when computing the desired screen orientation.boolean anim = true;final DisplayContent dc = getDisplay().mDisplayContent;if (prev != null) {if (prev.finishing) {if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,"Prepare close transition: prev=" + prev);if (mStackSupervisor.mNoAnimActivities.contains(prev)) {anim = false;dc.prepareAppTransition(TRANSIT_NONE, false);} else {dc.prepareAppTransition(prev.getTaskRecord() == next.getTaskRecord() ? TRANSIT_ACTIVITY_CLOSE: TRANSIT_TASK_CLOSE, false);}prev.setVisibility(false);} else {if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,"Prepare open transition: prev=" + prev);if (mStackSupervisor.mNoAnimActivities.contains(next)) {anim = false;dc.prepareAppTransition(TRANSIT_NONE, false);} else {dc.prepareAppTransition(prev.getTaskRecord() == next.getTaskRecord() ? TRANSIT_ACTIVITY_OPEN: next.mLaunchTaskBehind ? TRANSIT_TASK_OPEN_BEHIND: TRANSIT_TASK_OPEN, false);}}} else {if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");if (mStackSupervisor.mNoAnimActivities.contains(next)) {anim = false;dc.prepareAppTransition(TRANSIT_NONE, false);} else {dc.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);}}if (anim) {next.applyOptionsLocked();} else {next.clearOptionsLocked();}mStackSupervisor.mNoAnimActivities.clear();if (next.attachedToProcess()) {if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next+ " stopped=" + next.stopped + " visible=" + next.visible);// If the previous activity is translucent, force a visibility update of// the next activity, so that it's added to WM's opening app list, and// transition animation can be set up properly.// For example, pressing Home button with a translucent activity in focus.// Launcher is already visible in this case. If we don't add it to opening// apps, maybeUpdateTransitToWallpaper() will fail to identify this as a// TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.final boolean lastActivityTranslucent = lastFocusedStack != null&& (lastFocusedStack.inMultiWindowMode()|| (lastFocusedStack.mLastPausedActivity != null&& !lastFocusedStack.mLastPausedActivity.fullscreen));// This activity is now becoming visible.if (!next.visible || next.stopped || lastActivityTranslucent) {next.setVisibility(true);}// schedule launch ticks to collect information about slow apps.next.startLaunchTickingLocked();ActivityRecord lastResumedActivity =lastFocusedStack == null ? null : lastFocusedStack.mResumedActivity;final ActivityState lastState = next.getState();mService.updateCpuStats();if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next+ " (in existing)");next.setState(RESUMED, "resumeTopActivityInnerLocked");next.app.updateProcessInfo(false /* updateServiceConnectionActivities */,true /* activityChange */, true /* updateOomAdj */);updateLRUListLocked(next);// Have the window manager re-evaluate the orientation of// the screen based on the new activity order.boolean notUpdated = true;// Activity should also be visible if set mLaunchTaskBehind to true (see// ActivityRecord#shouldBeVisibleIgnoringKeyguard()).if (shouldBeVisible(next)) {// We have special rotation behavior when here is some active activity that// requests specific orientation or Keyguard is locked. Make sure all activity// visibilities are set correctly as well as the transition is updated if needed// to get the correct rotation behavior. Otherwise the following call to update// the orientation may cause incorrect configurations delivered to client as a// result of invisible window resize.// TODO: Remove this once visibilities are set correctly immediately when// starting an activity.notUpdated = !mRootActivityContainer.ensureVisibilityAndConfig(next, mDisplayId,true /* markFrozenIfConfigChanged */, false /* deferResume */);}if (notUpdated) {// The configuration update wasn't able to keep the existing// instance of the activity, and instead started a new one.// We should be all done, but let's just make sure our activity// is still at the top and schedule another run if something// weird happened.ActivityRecord nextNext = topRunningActivityLocked();if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,"Activity config changed during resume: " + next+ ", new next: " + nextNext);if (nextNext != next) {// Do over!mStackSupervisor.scheduleResumeTopActivities();}if (!next.visible || next.stopped) {next.setVisibility(true);}next.completeResumeLocked();return true;}try {final ClientTransaction transaction =ClientTransaction.obtain(next.app.getThread(), next.appToken);// Deliver all pending results.ArrayList<ResultInfo> a = next.results;if (a != null) {final int N = a.size();if (!next.finishing && N > 0) {if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,"Delivering results to " + next + ": " + a);transaction.addCallback(ActivityResultItem.obtain(a));}}if (next.newIntents != null) {transaction.addCallback(NewIntentItem.obtain(next.newIntents, true /* resume */));}// Well the app will no longer be stopped.// Clear app token stopped state in window manager if needed.next.notifyAppResumed(next.stopped);EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.mUserId,System.identityHashCode(next), next.getTaskRecord().taskId,next.shortComponentName);next.sleeping = false;mService.getAppWarningsLocked().onResumeActivity(next);next.app.setPendingUiCleanAndForceProcessStateUpTo(mService.mTopProcessState);next.clearOptionsLocked();transaction.setLifecycleStateRequest(ResumeActivityItem.obtain(next.app.getReportedProcState(),getDisplay().mDisplayContent.isNextTransitionForward()));mService.getLifecycleManager().scheduleTransaction(transaction);if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed "+ next);} catch (Exception e) {// Whoops, need to restart this activity!if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "+ lastState + ": " + next);next.setState(lastState, "resumeTopActivityInnerLocked");// lastResumedActivity being non-null implies there is a lastStack present.if (lastResumedActivity != null) {lastResumedActivity.setState(RESUMED, "resumeTopActivityInnerLocked");}Slog.i(TAG, "Restarting because process died: " + next);if (!next.hasBeenLaunched) {next.hasBeenLaunched = true;} else  if (SHOW_APP_STARTING_PREVIEW && lastFocusedStack != null&& lastFocusedStack.isTopStackOnDisplay()) {next.showStartingWindow(null /* prev */, false /* newTask */,false /* taskSwitch */);}mStackSupervisor.startSpecificActivityLocked(next, true, false);return true;}// From this point on, if something goes wrong there is no way// to recover the activity.try {next.completeResumeLocked();} catch (Exception e) {// If any exception gets thrown, toss away this// activity and try the next one.Slog.w(TAG, "Exception thrown during resume of " + next, e);requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,"resume-exception", true);return true;}} else {// Whoops, need to restart this activity!if (!next.hasBeenLaunched) {next.hasBeenLaunched = true;} else {if (SHOW_APP_STARTING_PREVIEW) {next.showStartingWindow(null /* prev */, false /* newTask */,false /* taskSwich */);}if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);}if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);①mStackSupervisor.startSpecificActivityLocked(next, true, true);}return true;}

由于此时应用进程还没有启动,所以最后调用的是mStackSupervisor的startSpecificActivityLocked方法

九、ActivityStackSupervisor

frameworks\\base\\services\\core\\java\\com\\android\\server\\wm\\ActivityStackSupervisor.java

9.1、startSpecificActivityLocked

 void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) {// Is this activity's application already running?final WindowProcessController wpc =mService.getProcessController(r.processName, r.info.applicationInfo.uid);boolean knownToBeDead = false;if (wpc != null && wpc.hasThread()) {try {realStartActivityLocked(r, wpc, andResume, checkConfig);return;} catch (RemoteException e) {Slog.w(TAG, "Exception when starting activity "+ r.intent.getComponent().flattenToShortString(), e);}// If a dead object exception was thrown -- fall through to// restart the application.knownToBeDead = true;}// Suppress transition until the new activity becomes ready, otherwise the keyguard can// appear for a short amount of time before the new process with the new activity had the// ability to set its showWhenLocked flags.if (getKeyguardController().isKeyguardLocked()) {r.notifyUnknownVisibilityLaunched();}try {if (Trace.isTagEnabled(TRACE_TAG_ACTIVITY_MANAGER)) {Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "dispatchingStartProcess:"+ r.processName);}// Post message to start process to avoid possible deadlock of calling into AMS with the// ATMS lock held.final Message msg = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess, mService.mAmInternal, r.processName,r.info.applicationInfo, knownToBeDead, "activity", r.intent.getComponent());mService.mH.sendMessage(msg);} finally {Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);}}

①同理此时应用进程还没有创建,不会进入下面逻辑
②这里会通过AMS通知Zygote创建应用进程

9.2、startProcess

        @Overridepublic void startProcess(String processName, ApplicationInfo info,boolean knownToBeDead, String hostingType, ComponentName hostingName) {try {if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "startProcess:"+ processName);}synchronized (ActivityManagerService.this) {startProcessLocked(processName, info, knownToBeDead, 0 /* intentFlags */,new HostingRecord(hostingType, hostingName),false /* allowWhileBooting */, false /* isolated */,true /* keepIfLarge */);}} finally {Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);}}@GuardedBy("this")final ProcessRecord startProcessLocked(String processName,ApplicationInfo info, boolean knownToBeDead, int intentFlags,HostingRecord hostingRecord, boolean allowWhileBooting,boolean isolated, boolean keepIfLarge) {return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,hostingRecord, allowWhileBooting, isolated, 0 /* isolatedUid */, keepIfLarge,null /* ABI override */, null /* entryPoint */, null /* entryPointArgs */,null /* crashHandler */);}

十、ProcessList

frameworks\\base\\services\\core\\java\\com\\android\\server\\am\\ProcessList.java

10.1、startProcessLocked

 @GuardedBy("mService")final void startProcessLocked(ProcessRecord app, HostingRecord hostingRecord) {startProcessLocked(app, hostingRecord, null /* abiOverride */);}
boolean startProcessLocked(ProcessRecord app, HostingRecord hostingRecord,boolean disableHiddenApiChecks, boolean mountExtStorageFull,String abiOverride) {if (app.pendingStart) {return true;}long startTime = SystemClock.elapsedRealtime();if (app.pid > 0 && app.pid != ActivityManagerService.MY_PID) {checkSlow(startTime, "startProcess: removing from pids map");mService.mPidsSelfLocked.remove(app);mService.mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);checkSlow(startTime, "startProcess: done removing from pids map");app.setPid(0);app.startSeq = 0;}if (DEBUG_PROCESSES && mService.mProcessesOnHold.contains(app)) Slog.v(TAG_PROCESSES,"startProcessLocked removing on hold: " + app);mService.mProcessesOnHold.remove(app);checkSlow(startTime, "startProcess: starting to update cpu stats");mService.updateCpuStats();checkSlow(startTime, "startProcess: done updating cpu stats");try {try {final int userId = UserHandle.getUserId(app.uid);AppGlobals.getPackageManager().checkPackageStartable(app.info.packageName, userId);} catch (RemoteException e) {throw e.rethrowAsRuntimeException();}int uid = app.uid;int[] gids = null;int mountExternal = Zygote.MOUNT_EXTERNAL_NONE;if (!app.isolated) {int[] permGids = null;try {checkSlow(startTime, "startProcess: getting gids from package manager");final IPackageManager pm = AppGlobals.getPackageManager();permGids = pm.getPackageGids(app.info.packageName,MATCH_DIRECT_BOOT_AUTO, app.userId);if (StorageManager.hasIsolatedStorage() && mountExtStorageFull) {mountExternal = Zygote.MOUNT_EXTERNAL_FULL;} else {StorageManagerInternal storageManagerInternal = LocalServices.getService(StorageManagerInternal.class);mountExternal = storageManagerInternal.getExternalStorageMountMode(uid,app.info.packageName);}} catch (RemoteException e) {throw e.rethrowAsRuntimeException();}/** Add shared application and profile GIDs so applications can share some* resources like shared libraries and access user-wide resources*/if (ArrayUtils.isEmpty(permGids)) {gids = new int[3];} else {gids = new int[permGids.length + 3];System.arraycopy(permGids, 0, gids, 3, permGids.length);}gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid));gids[1] = UserHandle.getCacheAppGid(UserHandle.getAppId(uid));gids[2] = UserHandle.getUserGid(UserHandle.getUserId(uid));// Replace any invalid GIDsif (gids[0] == UserHandle.ERR_GID) gids[0] = gids[2];if (gids[1] == UserHandle.ERR_GID) gids[1] = gids[2];}app.mountMode = mountExternal;checkSlow(startTime, "startProcess: building args");if (mService.mAtmInternal.isFactoryTestProcess(app.getWindowProcessController())) {uid = 0;}int runtimeFlags = 0;if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {runtimeFlags |= Zygote.DEBUG_ENABLE_JDWP;runtimeFlags |= Zygote.DEBUG_JAVA_DEBUGGABLE;// Also turn on CheckJNI for debuggable apps. It's quite// awkward to turn on otherwise.runtimeFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;// Check if the developer does not want ART verificationif (android.provider.Settings.Global.getInt(mService.mContext.getContentResolver(),android.provider.Settings.Global.ART_VERIFIER_VERIFY_DEBUGGABLE, 1) == 0) {runtimeFlags |= Zygote.DISABLE_VERIFIER;Slog.w(TAG_PROCESSES, app + ": ART verification disabled");}}// Run the app in safe mode if its manifest requests so or the// system is booted in safe mode.if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 ||mService.mSafeMode == true) {runtimeFlags |= Zygote.DEBUG_ENABLE_SAFEMODE;}if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_PROFILEABLE_BY_SHELL) != 0) {runtimeFlags |= Zygote.PROFILE_FROM_SHELL;}if ("1".equals(SystemProperties.get("debug.checkjni"))) {runtimeFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;}String genDebugInfoProperty = SystemProperties.get("debug.generate-debug-info");if ("1".equals(genDebugInfoProperty) || "true".equals(genDebugInfoProperty)) {runtimeFlags |= Zygote.DEBUG_GENERATE_DEBUG_INFO;}String genMiniDebugInfoProperty = SystemProperties.get("dalvik.vm.minidebuginfo");if ("1".equals(genMiniDebugInfoProperty) || "true".equals(genMiniDebugInfoProperty)) {runtimeFlags |= Zygote.DEBUG_GENERATE_MINI_DEBUG_INFO;}if ("1".equals(SystemProperties.get("debug.jni.logging"))) {runtimeFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING;}if ("1".equals(SystemProperties.get("debug.assert"))) {runtimeFlags |= Zygote.DEBUG_ENABLE_ASSERT;}if (mService.mNativeDebuggingApp != null&& mService.mNativeDebuggingApp.equals(app.processName)) {// Enable all debug flags required by the native debugger.runtimeFlags |= Zygote.DEBUG_ALWAYS_JIT;          // Don't interpret anythingruntimeFlags |= Zygote.DEBUG_GENERATE_DEBUG_INFO; // Generate debug inforuntimeFlags |= Zygote.DEBUG_NATIVE_DEBUGGABLE;   // Disbale optimizationsmService.mNativeDebuggingApp = null;}if (app.info.isEmbeddedDexUsed()|| (app.info.isPrivilegedApp()&& DexManager.isPackageSelectedToRunOob(app.pkgList.mPkgList.keySet()))) {runtimeFlags |= Zygote.ONLY_USE_SYSTEM_OAT_FILES;}if (!disableHiddenApiChecks && !mService.mHiddenApiBlacklist.isDisabled()) {app.info.maybeUpdateHiddenApiEnforcementPolicy(mService.mHiddenApiBlacklist.getPolicy());@ApplicationInfo.HiddenApiEnforcementPolicy int policy =app.info.getHiddenApiEnforcementPolicy();int policyBits = (policy << Zygote.API_ENFORCEMENT_POLICY_SHIFT);if ((policyBits & Zygote.API_ENFORCEMENT_POLICY_MASK) != policyBits) {throw new IllegalStateException("Invalid API policy: " + policy);}runtimeFlags |= policyBits;}String useAppImageCache = SystemProperties.get(PROPERTY_USE_APP_IMAGE_STARTUP_CACHE, "");// Property defaults to true currently.if (!TextUtils.isEmpty(useAppImageCache) && !useAppImageCache.equals("false")) {runtimeFlags |= Zygote.USE_APP_IMAGE_STARTUP_CACHE;}String invokeWith = null;if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {// Debuggable apps may include a wrapper script with their library directory.String wrapperFileName = app.info.nativeLibraryDir + "/wrap.sh";StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();try {if (new File(wrapperFileName).exists()) {invokeWith = "/system/bin/logwrapper " + wrapperFileName;}} finally {StrictMode.setThreadPolicy(oldPolicy);}}String requiredAbi = (abiOverride != null) ? abiOverride : app.info.primaryCpuAbi;if (requiredAbi == null) {requiredAbi = Build.SUPPORTED_ABIS[0];}String instructionSet = null;if (app.info.primaryCpuAbi != null) {instructionSet = VMRuntime.getInstructionSet(app.info.primaryCpuAbi);}app.gids = gids;app.setRequiredAbi(requiredAbi);app.instructionSet = instructionSet;// the per-user SELinux context must be setif (TextUtils.isEmpty(app.info.seInfoUser)) {Slog.wtf(ActivityManagerService.TAG, "SELinux tag not defined",new IllegalStateException("SELinux tag not defined for "+ app.info.packageName + " (uid " + app.uid + ")"));}final String seInfo = app.info.seInfo+ (TextUtils.isEmpty(app.info.seInfoUser) ? "" : app.info.seInfoUser);// Start the process.  It will either succeed and return a result containing// the PID of the new process, or else throw a RuntimeException.final String entryPoint = "android.app.ActivityThread";return startProcessLocked(hostingRecord, entryPoint, app, uid, gids,runtimeFlags, mountExternal, seInfo, requiredAbi, instructionSet, invokeWith,startTime);} catch (RuntimeException e) {Slog.e(ActivityManagerService.TAG, "Failure starting process " + app.processName, e);// Something went very wrong while trying to start this process; one// common case is when the package is frozen due to an active// upgrade. To recover, clean up any active bookkeeping related to// starting this process. (We already invoked this method once when// the package was initially frozen through KILL_APPLICATION_MSG, so// it doesn't hurt to use it again.)mService.forceStopPackageLocked(app.info.packageName, UserHandle.getAppId(app.uid),false, false, true, false, false, app.userId, "start failure");return false;}}

①启动应用进程以后,要进入的main方法

@GuardedBy("mService")boolean startProcessLocked(HostingRecord hostingRecord,String entryPoint,ProcessRecord app, int uid, int[] gids, int runtimeFlags, int mountExternal,String seInfo, String requiredAbi, String instructionSet, String invokeWith,long startTime) {app.pendingStart = true;app.killedByAm = false;app.removed = false;app.killed = false;if (app.startSeq != 0) {Slog.wtf(TAG, "startProcessLocked processName:" + app.processName+ " with non-zero startSeq:" + app.startSeq);}if (app.pid != 0) {Slog.wtf(TAG, "startProcessLocked processName:" + app.processName+ " with non-zero pid:" + app.pid);}final long startSeq = app.startSeq = ++mProcStartSeqCounter;app.setStartParams(uid, hostingRecord, seInfo, startTime);app.setUsingWrapper(invokeWith != null|| SystemProperties.get("wrap." + app.processName) != null);mPendingStarts.put(startSeq, app);if (mService.mConstants.FLAG_PROCESS_START_ASYNC) {if (DEBUG_PROCESSES) Slog.i(TAG_PROCESSES,"Posting procStart msg for " + app.toShortString());mService.mProcStartHandler.post(() -> {try {final Process.ProcessStartResult startResult = startProcess(app.hostingRecord,entryPoint, app, app.startUid, gids, runtimeFlags, mountExternal,app.seInfo, requiredAbi, instructionSet, invokeWith, app.startTime);synchronized (mService) {handleProcessStartedLocked(app, startResult, startSeq);}} catch (RuntimeException e) {synchronized (mService) {Slog.e(ActivityManagerService.TAG, "Failure starting process "+ app.processName, e);mPendingStarts.remove(startSeq);app.pendingStart = false;mService.forceStopPackageLocked(app.info.packageName,UserHandle.getAppId(app.uid),false, false, true, false, false, app.userId, "start failure");}}});return true;} else {try {final Process.ProcessStartResult startResult = startProcess(hostingRecord,entryPoint, app,uid, gids, runtimeFlags, mountExternal, seInfo, requiredAbi, instructionSet,invokeWith, startTime);handleProcessStartedLocked(app, startResult.pid, startResult.usingWrapper,startSeq, false);} catch (RuntimeException e) {Slog.e(ActivityManagerService.TAG, "Failure starting process "+ app.processName, e);app.pendingStart = false;mService.forceStopPackageLocked(app.info.packageName, UserHandle.getAppId(app.uid),false, false, true, false, false, app.userId, "start failure");}return app.pid > 0;}}

①最终调用的是startProcess

10.2、startProcess

 private Process.ProcessStartResult startProcess(HostingRecord hostingRecord, String entryPoint,ProcessRecord app, int uid, int[] gids, int runtimeFlags, int mountExternal,String seInfo, String requiredAbi, String instructionSet, String invokeWith,long startTime) {try {Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Start proc: " +app.processName);checkSlow(startTime, "startProcess: asking zygote to start proc");final Process.ProcessStartResult startResult;if (hostingRecord.usesWebviewZygote()) {startResult = startWebView(entryPoint,app.processName, uid, uid, gids, runtimeFlags, mountExternal,app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,app.info.dataDir, null, app.info.packageName,new String[] {PROC_START_SEQ_IDENT + app.startSeq});} else if (hostingRecord.usesAppZygote()) {final AppZygote appZygote = createAppZygoteForProcessIfNeeded(app);startResult = appZygote.getProcess().start(entryPoint,app.processName, uid, uid, gids, runtimeFlags, mountExternal,app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,app.info.dataDir, null, app.info.packageName,/*useUsapPool=*/ false,new String[] {PROC_START_SEQ_IDENT + app.startSeq});} else {startResult = Process.start(entryPoint,app.processName, uid, uid, gids, runtimeFlags, mountExternal,app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,app.info.dataDir, invokeWith, app.info.packageName,new String[] {PROC_START_SEQ_IDENT + app.startSeq});}checkSlow(startTime, "startProcess: returned from zygote!");return startResult;} finally {Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);}}

十一、Process

\\frameworks\\base\\core\\java\\android\\os\\Process.java

11.1、start

public static ProcessStartResult start(@NonNull final String processClass,@Nullable final String niceName,int uid, int gid, @Nullable int[] gids,int runtimeFlags,int mountExternal,int targetSdkVersion,@Nullable String seInfo,@NonNull String abi,@Nullable String instructionSet,@Nullable String appDataDir,@Nullable String invokeWith,@Nullable String packageName,@Nullable String[] zygoteArgs) {return ZYGOTE_PROCESS.start(processClass, niceName, uid, gid, gids,runtimeFlags, mountExternal, targetSdkVersion, seInfo,abi, instructionSet, appDataDir, invokeWith, packageName,/*useUsapPool=*/ true, zygoteArgs);}

十二、ZygoteProcess

frameworks\\base\\core\\java\\android\\os\\ZygoteProcess.java

12-1、start

public final Process.ProcessStartResult start(@NonNull final String processClass,final String niceName,int uid, int gid, @Nullable int[] gids,int runtimeFlags, int mountExternal,int targetSdkVersion,@Nullable String seInfo,@NonNull String abi,@Nullable String instructionSet,@Nullable String appDataDir,@Nullable String invokeWith,@Nullable String packageName,boolean useUsapPool,@Nullable String[] zygoteArgs) {// TODO (chriswailes): Is there a better place to check this value?if (fetchUsapPoolEnabledPropWithMinInterval()) {informZygotesOfUsapPoolStatus();}try {return startViaZygote(processClass, niceName, uid, gid, gids,runtimeFlags, mountExternal, targetSdkVersion, seInfo,abi, instructionSet, appDataDir, invokeWith, /*startChildZygote=*/ false,packageName, useUsapPool, zygoteArgs);} catch (ZygoteStartFailedEx ex) {Log.e(LOG_TAG,"Starting VM process through Zygote failed");throw new RuntimeException("Starting VM process through Zygote failed", ex);}}

12-2、startViaZygote

private Process.ProcessStartResult startViaZygote(@NonNull final String processClass,@Nullable final String niceName,final int uid, final int gid,@Nullable final int[] gids,int runtimeFlags, int mountExternal,int targetSdkVersion,@Nullable String seInfo,@NonNull String abi,@Nullable String instructionSet,@Nullable String appDataDir,@Nullable String invokeWith,boolean startChildZygote,@Nullable String packageName,boolean useUsapPool,@Nullable String[] extraArgs)throws ZygoteStartFailedEx {ArrayList<String> argsForZygote = new ArrayList<>();// --runtime-args, --setuid=, --setgid=,// and --setgroups= must go firstargsForZygote.add("--runtime-args");argsForZygote.add("--setuid=" + uid);argsForZygote.add("--setgid=" + gid);argsForZygote.add("--runtime-flags=" + runtimeFlags);if (mountExternal == Zygote.MOUNT_EXTERNAL_DEFAULT) {argsForZygote.add("--mount-external-default");} else if (mountExternal == Zygote.MOUNT_EXTERNAL_READ) {argsForZygote.add("--mount-external-read");} else if (mountExternal == Zygote.MOUNT_EXTERNAL_WRITE) {argsForZygote.add("--mount-external-write");} else if (mountExternal == Zygote.MOUNT_EXTERNAL_FULL) {argsForZygote.add("--mount-external-full");} else if (mountExternal == Zygote.MOUNT_EXTERNAL_INSTALLER) {argsForZygote.add("--mount-external-installer");} else if (mountExternal == Zygote.MOUNT_EXTERNAL_LEGACY) {argsForZygote.add("--mount-external-legacy");}argsForZygote.add("--target-sdk-version=" + targetSdkVersion);// --setgroups is a comma-separated listif (gids != null && gids.length > 0) {StringBuilder sb = new StringBuilder();sb.append("--setgroups=");int sz = gids.length;for (int i = 0; i < sz; i++) {if (i != 0) {sb.append(',');}sb.append(gids[i]);}argsForZygote.add(sb.toString());}if (niceName != null) {argsForZygote.add("--nice-name=" + niceName);}if (seInfo != null) {argsForZygote.add("--seinfo=" + seInfo);}if (instructionSet != null) {argsForZygote.add("--instruction-set=" + instructionSet);}if (appDataDir != null) {argsForZygote.add("--app-data-dir=" + appDataDir);}if (invokeWith != null) {argsForZygote.add("--invoke-with");argsForZygote.add(invokeWith);}if (startChildZygote) {argsForZygote.add("--start-child-zygote");}if (packageName != null) {argsForZygote.add("--package-name=" + packageName);}argsForZygote.add(processClass);if (extraArgs != null) {Collections.addAll(argsForZygote, extraArgs);}synchronized(mLock) {// The USAP pool can not be used if the application will not use the systems graphics// driver.  If that driver is requested use the Zygote application start path.return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi),useUsapPool,argsForZygote);}}

12-3、zygoteSendArgsAndGetResult

 /*** Sends an argument list to the zygote process, which starts a new child* and returns the child's pid. Please note: the present implementation* replaces newlines in the argument list with spaces.** @throws ZygoteStartFailedEx if process start failed for any reason*/@GuardedBy("mLock")private Process.ProcessStartResult zygoteSendArgsAndGetResult(ZygoteState zygoteState, boolean useUsapPool, @NonNull ArrayList<String> args)throws ZygoteStartFailedEx {// Throw early if any of the arguments are malformed. This means we can// avoid writing a partial response to the zygote.for (String arg : args) {// Making two indexOf calls here is faster than running a manually fused loop due// to the fact that indexOf is a optimized intrinsic.if (arg.indexOf('\\n') >= 0) {throw new ZygoteStartFailedEx("Embedded newlines not allowed");} else if (arg.indexOf('\\r') >= 0) {throw new ZygoteStartFailedEx("Embedded carriage returns not allowed");}}/** See com.android.internal.os.ZygoteArguments.parseArgs()* Presently the wire format to the zygote process is:* a) a count of arguments (argc, in essence)* b) a number of newline-separated argument strings equal to count** After the zygote process reads these it will write the pid of* the child or -1 on failure, followed by boolean to* indicate whether a wrapper process was used.*/String msgStr = args.size() + "\\n" + String.join("\\n", args) + "\\n";if (useUsapPool && mUsapPoolEnabled && canAttemptUsap(args)) {try {return attemptUsapSendArgsAndGetResult(zygoteState, msgStr);} catch (IOException ex) {// If there was an IOException using the USAP pool we will log the error and// attempt to start the process through the Zygote.Log.e(LOG_TAG, "IO Exception while communicating with USAP pool - "+ ex.getMessage());}}return attemptZygoteSendArgsAndGetResult(zygoteState, msgStr);}private Process.ProcessStartResult attemptZygoteSendArgsAndGetResult(ZygoteState zygoteState, String msgStr) throws ZygoteStartFailedEx {try {final BufferedWriter zygoteWriter = zygoteState.mZygoteOutputWriter;final DataInputStream zygoteInputStream = zygoteState.mZygoteInputStream;zygoteWriter.write(msgStr);zygoteWriter.flush();// Always read the entire result from the input stream to avoid leaving// bytes in the stream for future process starts to accidentally stumble// upon.Process.ProcessStartResult result = new Process.ProcessStartResult();result.pid = zygoteInputStream.readInt();result.usingWrapper = zygoteInputStream.readBoolean();if (result.pid < 0) {throw new ZygoteStartFailedEx("fork() failed");}return result;} catch (IOException ex) {zygoteState.close();Log.e(LOG_TAG, "IO Exception while communicating with Zygote - "+ ex.toString());throw new ZygoteStartFailedEx(ex);}}

该方法使用socket通信的方式,给Zygote进程发送数据。Zygote进程启动以后,就调用了runSelectLoop,死循环的等待接收数据。

十三、ZygoteServer

frameworks\\base\\core\\java\\com\\android\\internal\\os\\ZygoteServer.java

13.1、runSelectLoop

 Runnable runSelectLoop(String abiList) {ArrayList<FileDescriptor> socketFDs = new ArrayList<FileDescriptor>();ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>();socketFDs.add(mZygoteSocket.getFileDescriptor());peers.add(null);while (true) {fetchUsapPoolPolicyPropsWithMinInterval();int[] usapPipeFDs = null;StructPollfd[] pollFDs = null;// Allocate enough space for the poll structs, taking into account// the state of the USAP pool for this Zygote (could be a// regular Zygote, a WebView Zygote, or an AppZygote).if (mUsapPoolEnabled) {usapPipeFDs = Zygote.getUsapPipeFDs();pollFDs = new StructPollfd[socketFDs.size() + 1 + usapPipeFDs.length];} else {pollFDs = new StructPollfd[socketFDs.size()];}/** For reasons of correctness the USAP pool pipe and event FDs* must be processed before the session and server sockets.  This* is to ensure that the USAP pool accounting information is* accurate when handling other requests like API blacklist* exemptions.*/int pollIndex = 0;for (FileDescriptor socketFD : socketFDs) {pollFDs[pollIndex] = new StructPollfd();pollFDs[pollIndex].fd = socketFD;pollFDs[pollIndex].events = (short) POLLIN;++pollIndex;}final int usapPoolEventFDIndex = pollIndex;if (mUsapPoolEnabled) {pollFDs[pollIndex] = new StructPollfd();pollFDs[pollIndex].fd = mUsapPoolEventFD;pollFDs[pollIndex].events = (short) POLLIN;++pollIndex;for (int usapPipeFD : usapPipeFDs) {FileDescriptor managedFd = new FileDescriptor();managedFd.setInt$(usapPipeFD);pollFDs[pollIndex] = new StructPollfd();pollFDs[pollIndex].fd = managedFd;pollFDs[pollIndex].events = (short) POLLIN;++pollIndex;}}try {Os.poll(pollFDs, -1);} catch (ErrnoException ex) {throw new RuntimeException("poll failed", ex);}boolean usapPoolFDRead = false;while (--pollIndex >= 0) {if ((pollFDs[pollIndex].revents & POLLIN) == 0) {continue;}if (pollIndex == 0) {// Zygote server socket①ZygoteConnection newPeer = acceptCommandPeer(abiList);peers.add(newPeer);socketFDs.add(newPeer.getFileDescriptor());} else if (pollIndex < usapPoolEventFDIndex) {// Session socket accepted from the Zygote server socket②try {ZygoteConnection connection = peers.get(pollIndex);final Runnable command = connection.processOneCommand(this);// TODO (chriswailes): Is this extra check necessary?if (mIsForkChild) {// We're in the child. We should always have a command to run at this// stage if processOneCommand hasn't called "exec".if (command == null) {throw new IllegalStateException("command == null");}return command;} else {// We're in the server - we should never have any commands to run.if (command != null) {throw new IllegalStateException("command != null");}// We don't know whether the remote side of the socket was closed or// not until we attempt to read from it from processOneCommand. This// shows up as a regular POLLIN event in our regular processing loop.if (connection.isClosedByPeer()) {connection.closeSocket();peers.remove(pollIndex);socketFDs.remove(pollIndex);}}} catch (Exception e) {if (!mIsForkChild) {// We're in the server so any exception here is one that has taken place// pre-fork while processing commands or reading / writing from the// control socket. Make a loud noise about any such exceptions so that// we know exactly what failed and why.Slog.e(TAG, "Exception executing zygote command: ", e);// Make sure the socket is closed so that the other end knows// immediately that something has gone wrong and doesn't time out// waiting for a response.ZygoteConnection conn = peers.remove(pollIndex);conn.closeSocket();socketFDs.remove(pollIndex);} else {// We're in the child so any exception caught here has happened post// fork and before we execute ActivityThread.main (or any other main()// method). Log the details of the exception and bring down the process.Log.e(TAG, "Caught post-fork exception in child process.", e);throw e;}} finally {// Reset the child flag, in the event that the child process is a child-// zygote. The flag will not be consulted this loop pass after the Runnable// is returned.mIsForkChild = false;}} else {// Either the USAP pool event FD or a USAP reporting pipe.// If this is the event FD the payload will be the number of USAPs removed.// If this is a reporting pipe FD the payload will be the PID of the USAP// that was just specialized.long messagePayload = -1;try {byte[] buffer = new byte[Zygote.USAP_MANAGEMENT_MESSAGE_BYTES];int readBytes = Os.read(pollFDs[pollIndex].fd, buffer, 0, buffer.length);if (readBytes == Zygote.USAP_MANAGEMENT_MESSAGE_BYTES) {DataInputStream inputStream =new DataInputStream(new ByteArrayInputStream(buffer));messagePayload = inputStream.readLong();} else {Log.e(TAG, "Incomplete read from USAP management FD of size "+ readBytes);continue;}} catch (Exception ex) {if (pollIndex == usapPoolEventFDIndex) {Log.e(TAG, "Failed to read from USAP pool event FD: "+ ex.getMessage());} else {Log.e(TAG, "Failed to read from USAP reporting pipe: "+ ex.getMessage());}continue;}if (pollIndex > usapPoolEventFDIndex) {Zygote.removeUsapTableEntry((int) messagePayload);}usapPoolFDRead = true;}}// Check to see if the USAP pool needs to be refilled.if (usapPoolFDRead) {int[] sessionSocketRawFDs =socketFDs.subList(1, socketFDs.size()).stream().mapToInt(fd -> fd.getInt$()).toArray();final Runnable command = fillUsapPool(sessionSocketRawFDs);if (command != null) {return command;}}}}

①创建一个新的ZygoteConnection
②调用ZygoteConnection的processOneCommand方法,解析处理接收到的数据

十四、ZygoteConnection

frameworks\\base\\core\\java\\com\\android\\internal\\os\\ZygoteConnection.java

14.1、processOneCommand

 Runnable processOneCommand(ZygoteServer zygoteServer) {String args[];ZygoteArguments parsedArgs = null;FileDescriptor[] descriptors;try {args = Zygote.readArgumentList(mSocketReader);// TODO (chriswailes): Remove this and add an assert.descriptors = mSocket.getAncillaryFileDescriptors();} catch (IOException ex) {throw new IllegalStateException("IOException on command socket", ex);}// readArgumentList returns null only when it has reached EOF with no available// data to read. This will only happen when the remote socket has disconnected.if (args == null) {isEof = true;return null;}int pid = -1;FileDescriptor childPipeFd = null;FileDescriptor serverPipeFd = null;parsedArgs = new ZygoteArguments(args);if (parsedArgs.mAbiListQuery) {handleAbiListQuery();return null;}if (parsedArgs.mPidQuery) {handlePidQuery();return null;}if (parsedArgs.mUsapPoolStatusSpecified) {return handleUsapPoolStatusChange(zygoteServer, parsedArgs.mUsapPoolEnabled);}if (parsedArgs.mPreloadDefault) {handlePreload();return null;}if (parsedArgs.mPreloadPackage != null) {handlePreloadPackage(parsedArgs.mPreloadPackage, parsedArgs.mPreloadPackageLibs,parsedArgs.mPreloadPackageLibFileName, parsedArgs.mPreloadPackageCacheKey);return null;}if (canPreloadApp() && parsedArgs.mPreloadApp != null) {byte[] rawParcelData = Base64.getDecoder().decode(parsedArgs.mPreloadApp);Parcel appInfoParcel = Parcel.obtain();appInfoParcel.unmarshall(rawParcelData, 0, rawParcelData.length);appInfoParcel.setDataPosition(0);ApplicationInfo appInfo = ApplicationInfo.CREATOR.createFromParcel(appInfoParcel);appInfoParcel.recycle();if (appInfo != null) {handlePreloadApp(appInfo);} else {throw new IllegalArgumentException("Failed to deserialize --preload-app");}return null;}if (parsedArgs.mApiBlacklistExemptions != null) {return handleApiBlacklistExemptions(zygoteServer, parsedArgs.mApiBlacklistExemptions);}if (parsedArgs.mHiddenApiAccessLogSampleRate != -1|| parsedArgs.mHiddenApiAccessStatslogSampleRate != -1) {return handleHiddenApiAccessLogSampleRate(zygoteServer,parsedArgs.mHiddenApiAccessLogSampleRate,parsedArgs.mHiddenApiAccessStatslogSampleRate);}if (parsedArgs.mPermittedCapabilities != 0 || parsedArgs.mEffectiveCapabilities != 0) {throw new ZygoteSecurityException("Client may not specify capabilities: "+ "permitted=0x" + Long.toHexString(parsedArgs.mPermittedCapabilities)+ ", effective=0x" + Long.toHexString(parsedArgs.mEffectiveCapabilities));}Zygote.applyUidSecurityPolicy(parsedArgs, peer);Zygote.applyInvokeWithSecurityPolicy(parsedArgs, peer);Zygote.applyDebuggerSystemProperty(parsedArgs);Zygote.applyInvokeWithSystemProperty(parsedArgs);int[][] rlimits = null;if (parsedArgs.mRLimits != null) {rlimits = parsedArgs.mRLimits.toArray(Zygote.INT_ARRAY_2D);}int[] fdsToIgnore = null;if (parsedArgs.mInvokeWith != null) {try {FileDescriptor[] pipeFds = Os.pipe2(O_CLOEXEC);childPipeFd = pipeFds[1];serverPipeFd = pipeFds[0];Os.fcntlInt(childPipeFd, F_SETFD, 0);fdsToIgnore = new int[]{childPipeFd.getInt$(), serverPipeFd.getInt$()};} catch (ErrnoException errnoEx) {throw new IllegalStateException("Unable to set up pipe for invoke-with", errnoEx);}}int [] fdsToClose = { -1, -1 };FileDescriptor fd = mSocket.getFileDescriptor();if (fd != null) {fdsToClose[0] = fd.getInt$();}fd = zygoteServer.getZygoteSocketFileDescriptor();if (fd != null) {fdsToClose[1] = fd.getInt$();}fd = null;②pid = Zygote.forkAndSpecialize(parsedArgs.mUid, parsedArgs.mGid, parsedArgs.mGids,parsedArgs.mRuntimeFlags, rlimits, parsedArgs.mMountExternal, parsedArgs.mSeInfo,parsedArgs.mNiceName, fdsToClose, fdsToIgnore, parsedArgs.mStartChildZygote,parsedArgs.mInstructionSet, parsedArgs.mAppDataDir, parsedArgs.mTargetSdkVersion);try {if (pid == 0) {// in childzygoteServer.setForkChild();zygoteServer.closeServerSocket();IoUtils.closeQuietly(serverPipeFd);serverPipeFd = null;return handleChildProc(parsedArgs, descriptors, childPipeFd,parsedArgs.mStartChildZygote);} else {// In the parent. A pid < 0 indicates a failure and will be handled in// handleParentProc.IoUtils.closeQuietly(childPipeFd);childPipeFd = null;handleParentProc(pid, descriptors, serverPipeFd);return null;}} finally {IoUtils.closeQuietly(childPipeFd);IoUtils.closeQuietly(serverPipeFd);}}

①读取参数
②fork子进程
③子进程创建成功以后,处理子进程业务

14.2、handleChildProc

 private Runnable handleChildProc(ZygoteArguments parsedArgs, FileDescriptor[] descriptors,FileDescriptor pipeFd, boolean isZygote) {/*** By the time we get here, the native code has closed the two actual Zygote* socket connections, and substituted /dev/null in their place.  The LocalSocket* objects still need to be closed properly.*/closeSocket();if (descriptors != null) {try {Os.dup2(descriptors[0], STDIN_FILENO);Os.dup2(descriptors[1], STDOUT_FILENO);Os.dup2(descriptors[2], STDERR_FILENO);for (FileDescriptor fd: descriptors) {IoUtils.closeQuietly(fd);}} catch (ErrnoException ex) {Log.e(TAG, "Error reopening stdio", ex);}}if (parsedArgs.mNiceName != null) {Process.setArgV0(parsedArgs.mNiceName);}// End of the postFork event.Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);if (parsedArgs.mInvokeWith != null) {WrapperInit.execApplication(parsedArgs.mInvokeWith,parsedArgs.mNiceName, parsedArgs.mTargetSdkVersion,VMRuntime.getCurrentInstructionSet(),pipeFd, parsedArgs.mRemainingArgs);// Should not get here.throw new IllegalStateException("WrapperInit.execApplication unexpectedly returned");} else {if (!isZygote) {return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,parsedArgs.mRemainingArgs, null /* classLoader */);} else {return ZygoteInit.childZygoteInit(parsedArgs.mTargetSdkVersion,parsedArgs.mRemainingArgs, null /* classLoader */);}}}

①此时不是Zygote进程,所以调用zygoteInit方法

十五、ZygoteInit

frameworks\\base\\core\\java\\com\\android\\internal\\os\\ZygoteInit.java

15.1、zygoteInit

public static final Runnable zygoteInit(int targetSdkVersion, String[] argv,ClassLoader classLoader) {if (RuntimeInit.DEBUG) {Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote");}Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");RuntimeInit.redirectLogStreams();RuntimeInit.commonInit();ZygoteInit.nativeZygoteInit();return RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);}

最后调用RuntimeInit的applicationInit方法,该方法就是启动应用程序入口的main方法。

十六、RuntimeInit

\\frameworks\\base\\core\\java\\com\\android\\internal\\os\\RuntimeInit.java

16.1、applicationInit

 protected static Runnable applicationInit(int targetSdkVersion, String[] argv,ClassLoader classLoader) {// If the application calls System.exit(), terminate the process// immediately without running any shutdown hooks.  It is not possible to// shutdown an Android application gracefully.  Among other things, the// Android runtime shutdown hooks close the Binder driver, which can cause// leftover running threads to crash before the process actually exits.nativeSetExitWithoutCleanup(true);// We want to be fairly aggressive about heap utilization, to avoid// holding on to a lot of memory that isn't needed.VMRuntime.getRuntime().setTargetHeapUtilization(0.75f);VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);final Arguments args = new Arguments(argv);// The end of of the RuntimeInit event (see #zygoteInit).Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);// Remaining arguments are passed to the start class's static mainreturn findStaticMain(args.startClass, args.startArgs, classLoader);}protected static Runnable findStaticMain(String className, String[] argv,ClassLoader classLoader) {Class<?> cl;try {cl = Class.forName(className, true, classLoader);} catch (ClassNotFoundException ex) {throw new RuntimeException("Missing class when invoking static main " + className,ex);}Method m;try {m = cl.getMethod("main", new Class[] { String[].class });} catch (NoSuchMethodException ex) {throw new RuntimeException("Missing static main on " + className, ex);} catch (SecurityException ex) {throw new RuntimeException("Problem getting static main on " + className, ex);}int modifiers = m.getModifiers();if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {throw new RuntimeException("Main method is not public and static on " + className);}/** This throw gets caught in ZygoteInit.main(), which responds* by invoking the exception's run() method. This arrangement* clears up all the stack frames that were required in setting* up the process.*/return new MethodAndArgsCaller(m, argv);}

采用反射的方式,调用ActivityThread的main方法。至此,启动应用进程的流程到此为止,后面的流程就是启动Activity。