Step 7. ActivityStack.startActivityMayWait
这个函数定义在frameworks/base/services/java/com/Android/server/am/ActivityStack.java文件中:
publicclassActivityStack {
......
finalintstartActivityMayWait(IApplicationThread caller,
Intent intent, String resolvedType, Uri[] grantedUriPermissions,
intgrantedMode, IBinder resultTo,
String resultWho,intrequestCode,booleanonlyIfNeeded,
booleandebug, WaitResult outResult, Configuration config) {
......
booleancomponentSpecified = intent.getComponent() !=null;
// Don't modify the client's object!
intent =newIntent(intent);
// Collect information about the target of the Intent.
ActivityInfo aInfo;
try{
ResolveInfo rInfo =
AppGlobals.getPackageManager().resolveIntent(
intent, resolvedType,
PackageManager.MATCH_DEFAULT_ONLY
| ActivityManagerService.STOCK_PM_FLAGS);
aInfo = rInfo !=null? rInfo.activityInfo :null;
}catch(RemoteException e) {
......
}
if(aInfo !=null) {
// Store the found target back into the intent, because now that
// we have it we never want to do this again. For example, if the
// user navigates back to this point in the history, we should
// always restart the exact same activity.
intent.setComponent(newComponentName(
aInfo.applicationInfo.packageName, aInfo.name));
......
}
synchronized(mService) {
intcallingPid;
intcallingUid;
if(caller ==null) {
......
}else{
callingPid = callingUid = -1;
}
mConfigWillChange = config !=null
&& mService.mConfiguration.diff(config) !=0;
......
if(mMainStack && aInfo !=null&&
(aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) !=0) {
......
}
intres = startActivityLocked(caller, intent, resolvedType,
grantedUriPermissions, grantedMode, aInfo,
resultTo, resultWho, requestCode, callingPid, callingUid,
onlyIfNeeded, componentSpecified);
if(mConfigWillChange && mMainStack) {
......
}
......
if(outResult !=null) {
......
}
returnres;
}
}
......
} 注意,从Step 6传下来的参数outResult和config均为null,此外,表达式(aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0为false,因此,这里忽略了无关代码。
下面语句对参数intent的内容进行解析,得到MainActivity的相关信息,保存在aInfo变量中:
ActivityInfo aInfo;
try{
ResolveInfo rInfo =
AppGlobals.getPackageManager().resolveIntent(
intent, resolvedType,
PackageManager.MATCH_DEFAULT_ONLY
| ActivityManagerService.STOCK_PM_FLAGS);
aInfo = rInfo !=null? rInfo.activityInfo :null;
}catch(RemoteException e) {
......
} 解析之后,得到的aInfo.applicationInfo.packageName的值为"shy.luo.activity",aInfo.name的值为"shy.luo.activity.MainActivity",这是在这个实例的配置文件AndroidManifest.xml里面配置的。
此外,函数开始的地方调用intent.getComponent()函数的返回值不为null,因此,这里的componentSpecified变量为true。
接下去就调用startActivityLocked进一步处理了。
Step 8. ActivityStack.startActivityLocked
这个函数定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
publicclassActivityStack {
......
finalintstartActivityLocked(IApplicationThread caller,
Intent intent, String resolvedType,
Uri[] grantedUriPermissions,
intgrantedMode, ActivityInfo aInfo, IBinder resultTo,
String resultWho,intrequestCode,
intcallingPid,intcallingUid,booleanonlyIfNeeded,
booleancomponentSpecified) {
interr = START_SUCCESS;
ProcessRecord callerApp =null;
if(caller !=null) {
callerApp = mService.getRecordForAppLocked(caller);
if(callerApp !=null) {
callingPid = callerApp.pid;
callingUid = callerApp.info.uid;
}else{
......
}
}
......
ActivityRecord sourceRecord =null;
ActivityRecord resultRecord =null;
if(resultTo !=null) {
intindex = indexOfTokenLocked(resultTo);
......
if(index >=0) {
sourceRecord = (ActivityRecord)mHistory.get(index);
if(requestCode >=0&& !sourceRecord.finishing) {
......
}
}
}
intlaunchFlags = intent.getFlags();
if((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) !=0
&& sourceRecord !=null) {
......
}
if(err == START_SUCCESS && intent.getComponent() ==null) {
......
}
if(err == START_SUCCESS && aInfo ==null) {
......
}
if(err != START_SUCCESS) {
......
}
......
ActivityRecord r =newActivityRecord(mService,this, callerApp, callingUid,
intent, resolvedType, aInfo, mService.mConfiguration,
resultRecord, resultWho, requestCode, componentSpecified);
......
returnstartActivityUncheckedLocked(r, sourceRecord,
grantedUriPermissions, grantedMode, onlyIfNeeded,true);
}
......
} 从传进来的参数caller得到调用者的进程信息,并保存在callerApp变量中,这里就是Launcher应用程序的进程信息了。
前面说过,参数resultTo是Launcher这个Activity里面的一个Binder对象,通过它可以获得Launcher这个Activity的相关信息,保存在sourceRecord变量中。
再接下来,创建即将要启动的Activity的相关信息,并保存在r变量中:
ActivityRecord r =newActivityRecord(mService,this, callerApp, callingUid,
intent, resolvedType, aInfo, mService.mConfiguration,
resultRecord, resultWho, requestCode, componentSpecified); 接着调用startActivityUncheckedLocked函数进行下一步操作。