Activity 生命周期图:
启动应用进程
点击桌面启动应用过程
Activity的生命周期中只有在以下3种状态之一,才能较长时间内保持状态不变。
除此之外,其他状态都是过渡状态(或称为暂时状态),比如onCreate(),onStart()后很快就会调用onResume()方法
Instrumentation: 创建App
Instrumentation框架分析及其使用_OliverGao的博客-CSDN博客_instrumentation框架
点击launcher 启动
Android点击Launcher应用图标的应用程序启动过程(栈和进程的创建) - 简书
Android应用进程的创建过程 - 简书
BindApplication 过程
简述Activity生命周期 - 简书
AMS IApplicationThread
ProcessRecord windowProcessControl
ProcessList.java
final ProcessRecord startProcessLocked(String processName, ApplicationInfo info,
boolean knownToBeDead, int intentFlags, HostingRecord hostingRecord,
int zygotePolicyFlags, boolean allowWhileBooting, boolean isolated, int isolatedUid,
boolean keepIfLarge, String abiOverride, String entryPoint, String[] entryPointArgs,
Runnable crashHandler)
nativeMigrateToBoost 这个可以在创建进程是调度到大核;如果没用可以复用的ProcessRecord ,就需要创建它,复用的话需要清理对应的进程,ProcessRecord 其thread 域后面会跟ApplicationThread binder 绑定;设置crashHandler;系统没有准备好,则将需要启动的ProcessRecord加入等待列表。
startProcessLocked
创建好ProcessRecord,就调用startProcessLocked 启动进程,这里会将跟进pid 与ProcessRecord 映射保存mPidsSelfLocked;更新cpu 使用率;GL_oom 功能设计;启动进程入口entryPoint = "android.app.ActivityThread",调用startProcessLocked
startProcessLocked
这里可以调用 mService.mProcStartHandler.post(() -> handleProcessStart 异步启动进程;
同步启动
Process.ProcessStartResult startResult = startProcess
这里startProcess会根据ApplicationInfo和UId 创建AppZygote,这里就是保持跟Zygote的socket 连接;mAppZygotes、zygoteProcessList 保存着AppZygote及对应的ProcessRecord,下次可以直接用;
调用AppZygote通过AppZygoteInit与Zygote 通讯创建ChildZygoteProcess,再根据是WebZygote还是AppZygote模式或者默认模式,调用
appZygote.getProcess().start(entryPoint=com.android.internal.os.AppZygoteInit
或者Process.start(entryPoint=ActivityThread.main(),启动完handleProcessStartedLocked打印am_proc_start 事件,启动异常调用forceStopPackageLocked。
ZYGOTE_PROCESS.start(processClass=AppZygoteInit执行AppZygoteInit创建ChildZygoteProcess,再通过其调用start,接着调用startViaZygote(entryPoint=ActivityThread.main();Process.start调用startViaZygote(entryPoint=ActivityThread.main()。这里的差异就是前者需要创建SecondZygote再在创建的ChildZygote调用startViaZygote,创建执行应用进程。
handleProcessStartedLocked 打印am_proc_start
Zygote.forkAndSpecialize 创建应用进程,首先执行ZygoteHooks.preFork 停止下边4个守护线程
执行nativeForkAndSpecialize,其中ForkCommon 中通过fork创建子进程,子进程中会执行SpecializeCommon;后面执行postForkCommon。
SpecializeCommon 里面执行ActivityThread.main() env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, runtime_flags, is_system_server, is_child_zygote, managed_instruction_set);
打开coredump
点击桌面,会执行桌面当前Activity.java 的startActivityForResult ,在这里执行mInstrumentation.execStartActivity
frameworks\base\core\java\android\app\Instrumentation.java
execStartActivity --》
int result = ActivityTaskManager.getService().startActivity(whoThread,
who.getBasePackageName(), who.getAttributionTag(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()), token, target,
requestCode, 0, null, options);
checkStartActivityResult(result, intent);
whoThread 是launcher 对应的IApplicationThread
--》
public int startActivity(IApplicationThread caller, String callingPackage,
Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) {
return mActivityTaskManager.startActivity(caller, callingPackage, null, intent,
resolvedType, resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions);
}
@Override
public int startActivityWithFeature(IApplicationThread caller, String callingPackage,
String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo,
String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo,
Bundle bOptions) {
return mActivityTaskManager.startActivity(caller, callingPackage, callingFeatureId, intent,
resolvedType, resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions);
}
==》》
mActivityTaskManager.startActivity
--》》mActivityTaskManager.startActivityAsUser
return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
.setCaller(caller)
.setCallingPackage(callingPackage)
.setCallingFeatureId(callingFeatureId)
.setResolvedType(resolvedType)
.setResultTo(resultTo)
.setResultWho(resultWho)
.setRequestCode(requestCode)
.setStartFlags(startFlags)
.setProfilerInfo(profilerInfo)
.setActivityOptions(bOptions)
.setUserId(userId)
.execute();
--》》
ActivityStarter.execute
final ActivityRecord caller = ActivityRecord.forTokenLocked(mRequest.resultTo);
根据IApplicationToken早到ActivityRecord(这里是launcher 对应的)
--》》
res = executeRequest(mRequest);
callerApp = mService.getProcessController(caller);
根据IApplicationThread 获取WindowProcesserController,用于与window 通讯
--》》
mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
request.voiceInteractor, startFlags, true /* doResume */, checkedOptions, inTask, restrictedBgActivity, intentGrants);
--》》
ActivityStarter.startActivityInner
startedActivityStack = handleStartResult(r, result);
postStartActivityProcessing(r, result, startedActivityStack);
--》》
setInitialState
computeLaunchingTaskFlags
computeSourceStack
ActivityStack
mTargetStack.startActivityLocked(mStartActivity, topStack.getTopNonFinishingActivity(),
newTask, mKeepCurTransition, mOptions);
RootWindowContainer
resumeFocusedStacksTopActivities
--》》ActivityStack
resumeTopActivityUncheckedLocked
--》》ActivityStack
resumeTopActivityInnerLocked
--》》ActivityStackSupervisor
startSpecificActivity
--》》
mService.startProcessAsync
// Post message to start process to avoid possible deadlock of calling into AMS with the
// ATMS lock held.
final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess,
mAmInternal, activity.processName, activity.info.applicationInfo, knownToBeDead,isTop, hostingType, activity.intent.getComponent());mH.sendMessage(m);
---》》
AMS.startProcess-->startProcessLocked->mProcessList.startProcessLocked
http://events.jianshu.io/p/ed3158fc5631
https://blog.csdn.net/hzwailll/article/details/85339714
https://blog.csdn.net/zplxl99/article/details/104507480