linux4启动源码,Android应用程序启动过程源代码分析

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函数进行下一步操作。 0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值