1、概述
activity的启动过程中涉及到Android framework中的很多个层次,如活动管理者、窗口管理者、客户端应用APP。它们之间是如何实现一一对应的?
2、Token创建:
activity启动过程会去创建ActivityRecord对象记录该活动的信息,在执行构造函数就会去创建令牌Token
–>ActivityRecord.java
ActivityRecord(){
appToken = new Token(this, service);
}
3、Token传递到WMS过程:
当ActivityRecord创建好后,就会作为参数,去start该活动。该过程会将令牌token传递给WMS(窗口管理者)
–>ActivityStack.java
final void startActivityLocked(ActivityRecord r, boolean newTask,
boolean doResume, boolean keepCurTransition, Bundle options) {
......
mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
(r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
......
}
WMS(窗口管理者)会根据参数token,去创建对象AppWindowToken,并将AppWindowToken放入mTokenMap容器记录下来。当后面要addWindow时,就可以遍历mTokenMap,看是否存在对应的令牌token。
–>WindowManagerService.java
public void addAppToken(int addPos, IApplicationToken token...){
......
atoken = new AppWindowToken(this, token, voiceInteraction);
atoken.inputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
atoken.appFullscreen = fullscreen;
atoken.showForAllUsers = showForAllUsers;
atoken.requestedOrientation = requestedOrientation;
atoken.layoutConfigChanges = (configChanges &
(ActivityInfo.CONFIG_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) != 0;
atoken.mLaunchTaskBehind = launchTaskBehind;
if (DEBUG_TOKEN_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG, "addAppToken: " + atoken
+ " to stack=" + stackId + " task=" + taskId + " at " + addPos);
Task task = mTaskIdToTask.get(taskId);
if (task == null) {
task = createTaskLocked(taskId, stackId, userId, atoken);
}
task.addAppToken(addPos, atoken);
mTokenMap.put(token.asBinder(), atoken);
......
}
3、Token传递到应用客户端过程:
当AMS、WMS准备好后,就会通过scheduleLaunchActivity去发送消息,进行回调客户端执行activity生命周期的oncreate方法。此时会将activity的令牌r.appToken作为参数传递到客户端。
–>ActivityStackSupervisor.java
final boolean realStartActivityLocked(.....) {
......
app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
......
}
客户端scheduleLaunchActivity方法实现如下:
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) {
updateProcessState(procState, false);
ActivityClientRecord r = new ActivityClientRecord();
r.token = token;
......
sendMessage(H.LAUNCH_ACTIVITY, r);//发送信息执行oncreate方法
}
4、总结
通过以上分析,可以清楚地看出客户端、AMS、WMS分别只有activity的令牌token。通过令牌token,就可以确保activity在不同层次都可以保持一一对应的关系。