谈谈app的启动流程
2019-08-13 本文已影响0人
pioneerz
App的启动流程挺复杂的,涉及到很多东西,有System_server进程的Ams,Launcher进程的AMP等等。
大致流程

第一阶段:孵化进程
从图中我们可以看出,点击桌面Launcher上的图标之后,会调用startActivity,然后会走到AMS,最终经过一系列的操作初始化,会调用到Zygote进程的main方法,进而会调用到ActivityThread方法,到了这里就是我们所熟悉的东西了。
第二阶段:绑定应用程序和进程(android 版本26)
这部分的代码大部分都在ActivityManagerService.java中(一般在.../sdk/sources/android-xx/com/android/server/am目录下,主要是ActivityManagerServer和ActivityStackSupervisor这两个文件)
1.ActivityThread.java
public static void main(String[] args) {
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
Looper.loop();
}
private void attach(boolean system) {
// TODO 通过binder机制获取system_server进程的ams对象
final IActivityManager mgr = ActivityManager.getService();
try {
mgr.attachApplication(mAppThread);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
2.ActivityManagerService
public final void attachApplication(IApplicationThread thread) {
synchronized (this) {
int callingPid = Binder.getCallingPid();
final long origId = Binder.clearCallingIdentity();
attachApplicationLocked(thread, callingPid);
Binder.restoreCallingIdentity(origId);
}
}
private final boolean attachApplicationLocked(IApplicationThread thread,
int pid) {
// TODO 回调activityThread的bindApplication方法,进而回调到Application的onCreate方法进行初始化
thread.bindApplication(processName, appInfo, providers, null, profilerInfo,
null, null, null, testMode,
mBinderTransactionTrackingEnabled, enableTrackAllocation,
isRestrictedBackupMode || !normalMode, app.persistent,
new Configuration(getGlobalConfiguration()), app.compat,
getCommonServicesLocked(app.isolated),
mCoreSettingsObserver.getCoreSettingsLocked(),
buildSerial);
if (normalMode) {
try {
if (mStackSupervisor.attachApplicationLocked(app)) {
didSomething = true;
}
} catch (Exception e) {
}
}
return true;
}
boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
if (realStartActivityLocked(hr, app, true, true)) {
didSomething = true;
}
}
final boolean realStartActivityLocked(ActivityRecord r,
ProcessRecord app, boolean andResume, boolean checkConfig)
throws RemoteException {
app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
r.compat, r.launchedFromPackage, r.task.voiceInteractor, app.repProcState,
r.icicle, r.persistentState, results, newIntents, !andResume,
mService.isNextTransitionForward(), profilerInfo);
}
从这里我们可以看到又回调到了ActivityThread的scheduleLaunchActivity方法。
第三阶段:启动activity
@Override
public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
int procState, Bundle state, PersistableBundle persistentState,
List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
ActivityClientRecord r = new ActivityClientRecord();
r.token = token;
r.ident = ident;
r.intent = intent;
r.referrer = referrer;
r.voiceInteractor = voiceInteractor;
r.activityInfo = info;
r.compatInfo = compatInfo;
r.state = state;
r.persistentState = persistentState;
r.pendingResults = pendingResults;
r.pendingIntents = pendingNewIntents;
r.startsNotResumed = notResumed;
r.isForward = isForward;
r.profilerInfo = profilerInfo;
r.overrideConfig = overrideConfig;
updatePendingConfiguration(curConfig);
sendMessage(H.LAUNCH_ACTIVITY, r);
}
// TODO final H mH = new H();
private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
msg.arg1 = arg1;
msg.arg2 = arg2;
mH.sendMessage(msg);
}
// TODO 里面存在一个名为H的handler,这里只拷出部分相关代码
public void handleMessage(Message msg) {
switch (msg.what) {
case LAUNCH_ACTIVITY: {
final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo, r.compatInfo);
handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
} break;
}
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
// TODO 最终调用activity的onCreate方法
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
// TODO 最终调用activity的onResume方法
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
} else {
}
}
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
...
mInstrumentation.callActivityOnCreate(activity, r.state);
...
}
public void callActivityOnCreate(Activity activity, Bundle icicle) {
prePerformCreate(activity);
activity.performCreate(icicle);
postPerformCreate(activity);
}
final void performCreate(Bundle icicle) {
restoreHasCurrentPermissionRequest(icicle);
onCreate(icicle);
mActivityTransitionState.readState(icicle);
performCreateCommon();
}
部分代码已经在图中注释了,其他的生命周期和onCreate是相似的,就不多说了,可以自己查看源码。