一、概念
Android10(API29)开始,ActivityTaskManagerService 接管 ActivityManagerService。
二、启动ATMS过程
三、启动APP & 跳转Activity过程
3.1 Activity → ATMS
启动一个 APP 或跳转一个 Activity 都是调用的 startActivity(),进而调用 startActivityForResult()。
3.1.1 Activity
Activity{
public void startActivity(Intent intent, @Nullable Bundle options) {
startActivityForResult(intent, -1, options);
}
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
//委托给Instrumentation来启动
Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, options);
mMainThread.sendActivityResult(mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
}
}
3.1.2 Instrumentation
Instrumentation{
public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, String target, Intent intent, int requestCode, Bundle options) {
//获取一个Binder,它是ActivityTaskManagerService的proxy,然后调用startActivity()进行跨进程通信
int result = ActivityTaskManager.getService().startActivity(whoThread,
who.getOpPackageName(), who.getAttributionTag(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()), token, target,
requestCode, 0, null, options);
}
}
ActivityTaskManager{
public static IActivityTaskManager getService() {
return IActivityTaskManagerSingleton.get();
}
//一种单例模式
private static final Singleton<IActivityTaskManager> IActivityTaskManagerSingleton = new Singleton<IActivityTaskManager>() {
@Override
protected IActivityTaskManager create() {
//获取ATMS的代理对象Binder
final IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE);
return IActivityTaskManager.Stub.asInterface(b);
}
};
}
3.2 ATMS → PMS
ActivityTaskManagerService{
public final int startActivity(IApplicationThread caller, String callingPackage, String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) {
return startActivityAsUser(caller, callingPackage, callingFeatureId, intent, resolvedType, resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions, UserHandle.getCallingUserId());
}
private int startActivityAsUser(IApplicationThread caller, String callingPackage, @Nullable String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId, boolean validateIncomingUser) {
//获取ActivityStarter实例,去执行execute方法
return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
.setCaller(caller)
...
.execute();
}
}
ActivityStarter{
int execute() {
if (mRequest.activityInfo == null) {
mRequest.resolveActivity(mSupervisor);
}
}
static class Request {
void resolveActivity(ActivityTaskSupervisor supervisor) {
//AndroidManifest中intent标签
resolveInfo = supervisor.resolveIntent(intent, resolvedType, userId, 0, computeResolveFilterUid(callingUid, realCallingUid, filterCallingUid));
//转为Activity标签
activityInfo = supervisor.resolveActivity(intent, resolveInfo, startFlags, profilerInfo);
}
}
}
ActivityTaskSupervisor{
ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags, int filterCallingUid) {
//通过ATMS获取抽象类PackageManagerInternal
//PMS内部类PackageManagerInternalImpl实现了它(内部类持有外部类,就可以调用PMS的方法了)
//resolveIntent()会将从PMS的mPackages中查询到的ActivityInfo赋值给ResultInfo
mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType, modifiedFlags, privateResolveFlags, userId, true, filterCallingUid);
}
//将获取的ResultInfo转为ActivityInfo
ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags, ProfilerInfo profilerInfo, int userId, int filterCallingUid) {
final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId, 0, filterCallingUid);
return resolveActivity(intent, rInfo, startFlags, profilerInfo);
}
}