0%

Activity启动流程浅析

在api26中,ActivityManagerNative类已经被弃用,同时代理类ActivityManagerProxy也被删除了。

Instrumentation类的execStartActivity方法如下,注意注释1处。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, String resultWho,
Intent intent, int requestCode, Bundle options, UserHandle user) {
IApplicationThread whoThread = (IApplicationThread) contextThread;
if (mActivityMonitors != null) {
synchronized (mSync) {
final int N = mActivityMonitors.size();
for (int i=0; i<N; i++) {
final ActivityMonitor am = mActivityMonitors.get(i);
ActivityResult result = null;
if (am.ignoreMatchingSpecificIntents()) {
result = am.onStartActivity(intent);
}
if (result != null) {
am.mHits++;
return result;
} else if (am.match(who, null, intent)) {
am.mHits++;
if (am.isBlocking()) {
return requestCode >= 0 ? am.getResult() : null;
}
break;
}
}
}
}
try {
intent.migrateExtraStreamToClipData();
intent.prepareToLeaveProcess(who);
int result = ActivityManager.getService() //1
.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;
}
api 25中Instrumentation类的的execStartActivity方法如下,注意注释2处。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options) {
IApplicationThread whoThread = (IApplicationThread) contextThread;
Uri referrer = target != null ? target.onProvideReferrer() : null;
if (referrer != null) {
intent.putExtra(Intent.EXTRA_REFERRER, referrer);
}
if (mActivityMonitors != null) {
synchronized (mSync) {
final int N = mActivityMonitors.size();
for (int i=0; i<N; i++) {
final ActivityMonitor am = mActivityMonitors.get(i);
if (am.match(who, null, intent)) {
am.mHits++;
if (am.isBlocking()) {
return requestCode >= 0 ? am.getResult() : null;
}
break;
}
}
}
}
try {
intent.migrateExtraStreamToClipData();
intent.prepareToLeaveProcess(who);
int result = ActivityManagerNative.getDefault()//2
.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;
}

我们看到,在注释1处和注释2处代码不一样,接下来继续看不同部分的代码,api26:

1
2
3
4
5
6
7
8
9
10
11
12
13
public static IActivityManager getService() {
return IActivityManagerSingleton.get();
}

private static final Singleton<IActivityManager> IActivityManagerSingleton =
new Singleton<IActivityManager>() {
@Override
protected IActivityManager create() {
final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
final IActivityManager am = IActivityManager.Stub.asInterface(b);
return am;
}
};

api25:

1
2
3
static public IActivityManager getDefault() {
return gDefault.get();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {
protected IActivityManager create() {
IBinder b = ServiceManager.getService("activity");
if (false) {
Log.v("ActivityManager", "default service binder = " + b);
}
IActivityManager am = asInterface(b);
if (false) {
Log.v("ActivityManager", "default service = " + am);
}
return am;
}
};

这两个地方的代码返回的都是IActivityManager对象。但是不同的是:api25中使用的是静态方法asInterface,该方法属于ActivityManagerNative。asInterface的源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
static public IActivityManager asInterface(IBinder obj) {
if (obj == null) {
return null;
}
IActivityManager in =
(IActivityManager)obj.queryLocalInterface(descriptor);
if (in != null) {
return in;
}

return new ActivityManagerProxy(obj);
}

参考文章:Activity启动过程详解 说明了ActivityManagerNative类,还有ActivityManagerProxy类(已被移除),是如何实现和AMS进程通信的,最终使用了ActivityManagerProxy代理类实现了startActivity。
参考文章:ActivityManagerProxy已经被删除

api26使用的是:

1
IActivityManager.Stub.asInterface(b);

再看api26中的ActivityManagerNative源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* {@hide}
* @deprecated will be removed soon. See individual methods for alternatives. //将会被移除
*/
@Deprecated
public abstract class ActivityManagerNative {
/**
* Cast a Binder object into an activity manager interface, generating
* a proxy if needed.
*
* @deprecated use IActivityManager.Stub.asInterface instead.
*/ //这里说可以用IActivityManager.Stub.asInterface来代替,这个方法已不被鼓励使用
static public IActivityManager asInterface(IBinder obj) {
return IActivityManager.Stub.asInterface(obj);
}

/**
* Retrieve the system's default/global activity manager.
*
* @deprecated use ActivityManager.getService instead.
*/
static public IActivityManager getDefault() {
return ActivityManager.getService();
}

/**
* Convenience for checking whether the system is ready. For internal use only.
*
* @deprecated use ActivityManagerInternal.isSystemReady instead.
*/
static public boolean isSystemReady() {
return ActivityManager.isSystemReady();
}

/**
* @deprecated use ActivityManager.broadcastStickyIntent instead.
*/
static public void broadcastStickyIntent(Intent intent, String permission, int userId) {
broadcastStickyIntent(intent, permission, AppOpsManager.OP_NONE, userId);
}

/**
* Convenience for sending a sticky broadcast. For internal use only.
* If you don't care about permission, use null.
*
* @deprecated use ActivityManager.broadcastStickyIntent instead.
*/
static public void broadcastStickyIntent(Intent intent, String permission, int appOp,
int userId) {
ActivityManager.broadcastStickyIntent(intent, appOp, userId);
}

/**
* @deprecated use ActivityManager.noteWakeupAlarm instead.
*/
static public void noteWakeupAlarm(PendingIntent ps, int sourceUid, String sourcePkg,
String tag) {
ActivityManager.noteWakeupAlarm(ps, sourceUid, sourcePkg, tag);
}

/**
* @deprecated use ActivityManager.noteAlarmStart instead.
*/
static public void noteAlarmStart(PendingIntent ps, int sourceUid, String tag) {
ActivityManager.noteAlarmStart(ps, sourceUid, tag);
}

/**
* @deprecated use ActivityManager.noteAlarmFinish instead.
*/
static public void noteAlarmFinish(PendingIntent ps, int sourceUid, String tag) {
ActivityManager.noteAlarmFinish(ps, sourceUid, tag);
}
}

“@deprecated will be removed soon. See individual methods for alternatives. “上面注释说明这个类将会被弃用,我们可以用ActivityManager来替代,而且ActivityManagerProxy已经被删除了。

api26及以后启动activity时候,和AMS通信已经采用了AIDL方式。实际上api25和api26及之后,启动Activity启动Activity的时候,和AMS通信调用的也是queryLocalInterface,他们的实质还是一样的。

api26及之后只有ServiceManagerNative还没有被弃用,api25的:ApplicationThreadNative、ActivityManagerNative、BulkCursorNative、ContentProviderNative都已经不再使用了。

这两句代码:mInstrumentation.execStartActivity,mMainThread.sendActivityResult。是activity启动过程中很重要的核心逻辑。

在ActivityThread中一个Activity被启动时相关方法是这样的:schedule–>handle–>perform–>activity/Insturmation。