android 7.1 ams启动,android AMS—— Activity的启动一

Activity的启动由APP端发起,通过Binder通讯进入系统端的AMS。

Acitivity的启动.png

一、APP端发起

从APP端点击应用打开窗口调用StartActivity开始流程经过Instrumentation. execStartActivity中ActivityManager.getService()获得AMS IBinder客户端,并与AMS进行交互,进入到AMS中的startActivity。

二、AMS端

1、ActivityStartController&&ActivityStarter

从名字上看两者,ActivityStartController应该是作为ActivityStarter的Control类存在的,在AMS.startActivityAsUser中通过ActivityStartController来启动创建Activity的流程

/*AMS.java*/

public final int startActivityAsUser(IApplicationThread caller, String callingPackage,

Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,

int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId,

boolean validateIncomingUser) {

enforceNotIsolatedCaller("startActivity");

userId = mActivityStartController.checkTargetUser(userId, validateIncomingUser,

Binder.getCallingPid(), Binder.getCallingUid(), "startActivityAsUser");

// TODO: Switch to user app stacks here.

return mActivityStartController.obtainStarter(intent, "startActivityAsUser")

.setCaller(caller)

.setCallingPackage(callingPackage)

.setResolvedType(resolvedType)

.setResultTo(resultTo)

.setResultWho(resultWho)

.setRequestCode(requestCode)

.setStartFlags(startFlags)

.setProfilerInfo(profilerInfo)

.setActivityOptions(bOptions)

.setMayWait(userId)

.execute();

}

从代码调用中可以看出其类似 建造者设计模式的方式,其ActivityStartController中采用了通过ActivityStarter.DefaultFactory采用享元模式共享ActivityStarter对象池。

ActivityStater.png

通过ActivityStartController.obtainStarter从mFactory中的ActivityStarter线程池中获取已有或者新创建一个ActivityStater对象,通过链式调用进行赋值后调用execute执行。进入ActivityStater.startActivityMayWait。

ActivityStarter.startActivityMayWait:主要解析出与Intent相匹配的ActivityInfo

从PackageManagerService准备activity需要的数据。查找PMS中已有的ResolveInfo,通过intent如果匹配到多个则用户选择,没匹配到则从启动中获取ResolveInfo。并通过通过PMS的getActivityInfo读取ActivityInfo。

对重量进程处理

进入下一步启动调用startActivity

并将解析的ActivityInfo信息传入到startActivity()方法为目标Activity准备好了ActivityRecord,并将新创建的ActivityRecord作为参数传入另一个startActivity,再进入startActivityUnchecked,

startActivityUnchecked: 该函数主要针对Activity栈进行处理,通过对intent中flag的分析选择TaskRecord以及ActivityStack,或者创建TaskRecord以及ActivityStack > 创建流程另外分析吧)

private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,

IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,

int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,

ActivityRecord[] outActivity) {

//注释1: 整个参数赋值给ActivityStarter的全局变量

setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,

voiceInteractor);

//注释2: 计算Task的flag

computeLaunchingTaskFlags();

//注释3:获取调用方的Activity栈

computeSourceStack();

mIntent.setFlags(mLaunchFlags);

ActivityRecord reusedActivity = getReusableIntentActivity();

......

}

注释1:setInitialState 整个参数赋值给ActivityStarter的全局变量,并对Intent中的flag进行一些处理,其中主要处理FLAG_ACTIVITY_NEW_DOCUMENT的

2、ActivityStack.resumeTopActivityInnerLocked

Activity-pause.png

ActivityStart中通过ActivityStackSupervisor.resumeFocusedStackTopActivityLocked处理mFocusedStack.resumeTopActivityUncheckedLocked进入到FocusStatck的切换处理resumeTopActivityInnerLocked:

resumeTopActivityInnerLocked该函数主要完成:

调用该ActivityStatck中的栈内所有Activity的onPause

从上图可以看出通过ActivityStackSupervisor.pauseBackStacks循环获取ActivityStackSupervisor中的ActivityDisplay中的ActivityStack,然后调用每个ActivityStack的startPausingLocked方法

final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,

ActivityRecord resuming, boolean pauseImmediately) {

......

mService.getLifecycleManager().scheduleTransaction(prev.app.thread, prev.appToken,

PauseActivityItem.obtain(prev.finishing, userLeaving,

prev.configChangeFlags, pauseImmediately));

......

}

检查ActivityRecord,若为复用对象,则通过创建ClientTransaction对象以及NewIntentItem、ResumeActivityItem事务完成onResume的调用

if (next.app != null && next.app.thread != null) {

//找到下一个需要启动的对象已经存在,则只需要resume

......

final ClientTransaction transaction = ClientTransaction.obtain(next.app.thread,

next.appToken);

ArrayList a = next.results;

if (a != null) {

final int N = a.size();

if (!next.finishing && N > 0) {

transaction.addCallback(ActivityResultItem.obtain(a));

}

}

if (next.newIntents != null) {

transaction.addCallback(NewIntentItem.obtain(next.newIntents,

false /* andPause */));

}

transaction.setLifecycleStateRequest(

ResumeActivityItem.obtain(next.app.repProcState,

mService.isNextTransitionForward()));

......

}else{

//下一个对象为空,则说明新建的ActivityRecord并没有与APP端绑定

mStackSupervisor.startSpecificActivityLocked(next, true, true);

}

ActivityStackSupervisor.startSpecificActivityLocked

对于新开一个Activity,最终是进入该函数,由该函数进入realStartActivityLocked开始真正的新Activity的生命周期的流程。

你可能感兴趣的:(android,7.1,ams启动)