/**
* Base classfor implementing application instrumentation code. When running
* with instrumentation turned on, this class will be instantiated for you
* before any oftheapplication code, allowing you to monitor all ofthe
* interaction the system has withtheapplication. An Instrumentation
* implementation is described tothe system through an AndroidManifest.xml's
* <instrumentation> tag.
*/
public class Instrumentation {
publicvoidstartActivityForResult(@RequiresPermission Intent intent, int requestCode,
@Nullable Bundle options) {
if (mParent == null) {
options = transferSpringboardActivityOptions(options);
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
//这里的mMainThread也是Activity类的成员变量,//它的类型是ActivityThread,它代表的是应用程序的主线程//这里通过mMainThread.getApplicationThread获得它里面的ApplicationThread成员变量,//它是一个Binder对象,后面我们会看到,ActivityManagerService会//使用它来和ActivityThread来进行进程间通信。这里我们需注意的是,这里的mMainThread代表的是Launcher应用程序运行的进程。
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options) {
IApplicationThread whoThread = (IApplicationThread) contextThread;
if (mActivityMonitors != null) {
synchronized (mSync) {
finalint 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();
int result = ActivityManagerNative.getDefault()
.startActivity(whoThread, who.getBasePackageName(), intent,//实际执行的是ActivityManagerService
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, null, options);
checkStartActivityResult(result, intent);//检查activity执行结果
} catch (RemoteException e) {
}
returnnull;
}
/**
* Retrieve the system's default/global activity manager.
*/staticpublic IActivityManager getDefault() {
return gDefault.get();
}
privatestaticfinal 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;
}
};
/**
* Interact with the overall activities running in the system.
*/publicclassActivityManager {privatestatic String TAG = "ActivityManager";
privatestaticboolean localLOGV = false;
privatefinal Context mContext;
privatefinal Handler mHandler;
}
publicfinalclassActivityManagerServiceextendsActivityManagerNativeimplementsWatchdog.Monitor, BatteryStatsImpl.BatteryCallback {@OverridepublicfinalintstartActivity(IApplicationThread caller, String callingPackage,
Intent intent, String resolvedType, IBinder resultTo,
String resultWho, int requestCode, int startFlags,
String profileFile, ParcelFileDescriptor profileFd, Bundle options) {
return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
resultWho, requestCode,
startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId());
}
}
//常见概念//Application /** * Base class for implementing application instrumentation code. When running * with instrumentation turned on, this class will be instantiated for you * before any of...