上篇文章Activity启动流程-基于Android10(一)已经讲到了 Instrumentation的execStartActivity方法。其实Android10版本和之前版本的代码是有区别。
execStartActivity:
frameworks\base\core\java\android\app\Instrumentation.java
@UnsupportedAppUsage
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, String resultWho,
Intent intent, int requestCode, Bundle options, UserHandle user) {
//...省略部分代码
try {
intent.migrateExtraStreamToClipData();
intent.prepareToLeaveProcess(who);
// 重要代码
int result = ActivityTaskManager.getService()
.startActivityAsUser(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, resultWho,
requestCode, 0, null, options, user.getIdentifier());
checkStartActivityResult(result, intent);
} catch (RemoteException e) {
throw new RuntimeException("Failure from system", e);
}
return null;
//...省略部分代码
代码调用了ActivityTaskManager的getService方法,让我们看下里面的逻辑处理
frameworks\base\core\java\android\app\ActivityTaskManager.java
/** @hide */
public static IActivityTaskManager getService() {
return IActivityTaskManagerSingleton.get();
}
@UnsupportedAppUsage(trackingBug = 129726065)
private static final Singleton<IActivityTaskManager> IActivityTaskManagerSingleton =
new Singleton<IActivityTaskManager>() {
@Override
protected IActivityTaskManager create() {
final IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE);
return IActivityTaskManager.Stub.asInterface(b);
}
};
(关于Singleton类的讲解可以参考我的这篇文章Android源码随心看系列:Singleton)
通过代码可以看出getService是获取了IActivityTaskManager.aidl接口,那么是哪个类实现了这个接口呢?答案是ActivityTaskManangerService。代码如下:
frameworks\base\services\core\java\com\android\server\wm\ActivityTaskManagerService.java
// ActivityTaskManagerService继承IActivityTaskManager.Stub
// IActivityTaskManager.Stub是Android根据aidl文件生成的java代码
public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityTaskManagerService" : TAG_ATM;
private static final String TAG_STACK = TAG + POSTFIX_STACK;
private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
private static final String TAG_IMMERSIVE = TAG + POSTFIX_IMMERSIVE;
private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
//...省略部分代码
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("startActivityAsUser");
userId = getActivityStartController().checkTargetUser(userId, validateIncomingUser,
Binder.getCallingPid(), Binder.getCallingUid(), "startActivityAsUser");
// TODO: Switch to user app stacks here.
return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
.setCaller(caller)
.setCallingPackage(callingPackage)
.setResolvedType(resolvedType)
.setResultTo(resultTo)
.setResultWho(resultWho)
.setRequestCode(requestCode)
.setStartFlags(startFlags)
.setProfilerInfo(profilerInfo)
.setActivityOptions(bOptions)
.setMayWait(userId)
.execute();
}
//...省略部分代码
最后调用的是ActivityTaskManagerService中的startActivityAsUser方法。通过分析可以知道了IActivityTaskManager.aidl就是Activity和ActivityTaskManagerService之间的桥梁。Activity的startActivity最终是调用了ActivityTaskManagerService中的startActivityAsUser方法
简化流程示意图:
Activity .java ⇒ I A c t i v i t y T a s k M a n a g e r . a i d l \xRightarrow{IActivityTaskManager.aidl} IActivityTaskManager.aidlActivityTaskManagerService.java
以上讲的是基于Android10的IPC,低于Android10的版本的IPC在细节上有些不一样,但是本质上是相同的。
还是回到execStartActivity方法:
frameworks\base\core\java\android\app\Instrumentation.java
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options) {
...
try {
intent.migrateExtraStreamToClipData();
intent.prepareToLeaveProcess(who);
// 重要代码
int result = ActivityManager.getService()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, options);
checkStartActivityResult(result, intent);
} catch (RemoteException e) {
throw new RuntimeException("Failure from system", e);
}
return null;
}
老版本用的是ActivityManager类,但是代码结构和Android10的ActivityTaskManager是一致。
ActivityManager:
platform\frameworks_base\core\java\android\app\ActivityManager.java
/**
* @hide
*/
@UnsupportedAppUsage
public static IActivityManager getService() {
return IActivityManagerSingleton.get();
}
private static IActivityTaskManager getTaskService() {
return ActivityTaskManager.getService();
}
@UnsupportedAppUsage
private static final Singleton<IActivityManager> IActivityManagerSingleton =
new Singleton<IActivityManager>() {
@Override
protected IActivityManager create() {
// 这里的IBinder其实是IActivityManager.aidl接口
final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
final IActivityManager am = IActivityManager.Stub.asInterface(b);
return am;
}
};
老版本用的是IActivityManager.aidl做IPC通讯的。大名鼎鼎的ActivityManagerService继承IActivityManger.aidl接口。所以老版本(Android 8.0及其以上)用IActivityManger做为IPC接口
简化流程示意图:
Activity .java ⇒ I A c t i v i t y M a n a g e r . a i d l \xRightarrow{IActivityManager.aidl} IActivityManager.aidlActivityManagerService.java
下一篇:Activity启动源码分析-基于Android10(三)
参考文章:Activity的启动流程-基于Android10源码