publicstaticIActivityTaskManagergetService(){returnIActivityTaskManagerSingleton.get();}//返回ActivityTaskManagerService@UnsupportedAppUsage(trackingBug =129726065)privatestaticfinalSingleton<IActivityTaskManager>IActivityTaskManagerSingleton=newSingleton<IActivityTaskManager>(){@OverrideprotectedIActivityTaskManagercreate(){finalIBinder b =ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE);returnIActivityTaskManager.Stub.asInterface(b);}};
//判断是否调用deliverNewIntent()通知Activity有一个Intent视图启动它if(topStack !=null){
startResult =deliverToCurrentTopIfNeeded(topStack, intentGrants);if(startResult != START_SUCCESS){return startResult;}}if(mDoResume){finalActivityRecord topTaskActivity = mStartActivity.getTask().topRunningActivityLocked();if(!mTargetStack.isTopActivityFocusable()||(topTaskActivity !=null&& topTaskActivity.isTaskOverlay()&& mStartActivity != topTaskActivity)){
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.isTopActivityFocusable()&&!mRootWindowContainer.isTopDisplayFocusedStack(mTargetStack)){
mTargetStack.moveToFront("startActivityInner");}//开始resume我们的Activity
mRootWindowContainer.resumeFocusedStacksTopActivities(
mTargetStack, mStartActivity, mOptions);}}
boolean result =false;if(targetStack !=null&&(targetStack.isTopStackInDisplayArea()||getTopDisplayFocusedStack()== targetStack)){
result = targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);}
voidstartProcessAsync(ActivityRecord activity,boolean knownToBeDead,boolean isTop,String hostingType){try{if(Trace.isTagEnabled(TRACE_TAG_WINDOW_MANAGER)){Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,"dispatchingStartProcess:"+ activity.processName);}// Post message to start process to avoid possible deadlock of calling into AMS with the// ATMS lock held.finalMessage m =/**
** 1.ActivityManagerInternal是一个抽象类,其实现类是ActiManagerService里的LocalService
** 2.一个规律:在源码里面发现类似xxxInternal的抽象类,实现类往往是xxxService里面的LocalService
**/PooledLambda.obtainMessage(ActivityManagerInternal::startProcess,mAmInternal, activity.processName, activity.info.applicationInfo, knownToBeDead, isTop, hostingType, activity.intent.getComponent());
mH.sendMessage(m);}finally{Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);}}
// Start the process. It will either succeed and return a result containing// the PID of the new process, or else throw a RuntimeException.finalString entryPoint ="android.app.ActivityThread";returnstartProcessLocked(hostingRecord, entryPoint, app, uid, gids, runtimeFlags, zygotePolicyFlags, mountExternal, seInfo, requiredAbi, instructionSet, invokeWith, startTime);
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.returnzygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi),
zygotePolicyFlags,
argsForZygote);}
//USAP进程启动优化机制//判断是使用usap启动app进程还是直接forkif(shouldAttemptUsapLaunch(zygotePolicyFlags, args)){try{returnattemptUsapSendArgsAndGetResult(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());}}returnattemptZygoteSendArgsAndGetResult(zygoteState, msgStr);
ZygoteProcess.attemptUsapSendArgsAndGetResult()
//向zygote进程发送消息请求创建app进程privateProcess.ProcessStartResultattemptUsapSendArgsAndGetResult(ZygoteState zygoteState,String msgStr)throwsZygoteStartFailedEx,IOException{try(LocalSocket usapSessionSocket = zygoteState.getUsapSessionSocket()){finalBufferedWriter usapWriter =newBufferedWriter(newOutputStreamWriter(usapSessionSocket.getOutputStream()),Zygote.SOCKET_BUFFER_SIZE);finalDataInputStream usapReader =newDataInputStream(usapSessionSocket.getInputStream());
usapWriter.write(msgStr);
usapWriter.flush();Process.ProcessStartResult result =newProcess.ProcessStartResult();
result.pid = usapReader.readInt();// USAPs can't be used to spawn processes that need wrappers.
result.usingWrapper =false;if(result.pid >=0){return result;}else{thrownewZygoteStartFailedEx("USAP specialization failed");}}}