一、四大組件的運行狀態
android四大組件中國除了BroadcastReceiver以外,其他三種都必須在AndroidManifest中註冊,對於BroadcastReceiver既可以在AndroidManifest中註冊也可以通過代碼註冊。
在調用方式上,activity、service和BroadcastReceiver需要藉助Intent,而ContentProvider則無須藉助Intent
二、Activity的工作過程
Activity是一個很重要的組件,啓動Activity很簡單,只需要
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
startActivity方法有很多種重載方式,但是最終都會調用startActivityForResult方法,他的實現如下:
public void startActivityForResult(@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);
if (ar != null) {
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
if (requestCode >= 0) {
// If this start is requesting a result, we can avoid making
// the activity visible until the result is received. Setting
// this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
// activity hidden during this time, to avoid flickering.
// This can only be done when a result is requested because
// that guarantees we will get information back when the
// activity is finished, no matter what happens to it.
mStartedActivity = true;
}
cancelInputsAndStartExitTransition(options);
// TODO Consider clearing/flushing other event sources and events for child windows.
} else {
if (options != null) {
mParent.startActivityFromChild(this, intent, requestCode, options);
} else {
// Note we want to go through this method for compatibility with
// existing applications that may have overridden it.
mParent.startActivityFromChild(this, intent, requestCode);
}
}
}
上面代碼中,需要注意mMainThread.getApplicationThread()這個參數,他的類型是ApplicationThread,ApplicationThread是ActivityThread的一個內部類,接着看一下mInstrumentation.execStartActivity方法
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);
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()
.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;
}
通過上面代碼可以看出,啓動Activity真正的實現由ActivityManager.getService()startActivity來完成。
checkStartActivityResult的作用很明顯就是檢查啓動activity的結果
下面分析一下AMS的startActivity方法,如下所示
public final int startActivity(IApplicationThread caller,String callingPackage,Intent intent,String resolvedType,IBinder resultTo,String resultWho,int requestCode,int startFlags,ProfilerInfo profilerInfo,Bundle options){
return startActivityAsUser(caller,callingPackage,intent,resolvedType,resultTo,resultWho,requestCode,startFlags,profilerInfo,options,UserHandle.getCallingUserId());
}
public final int startActivityAsUser(IApplicationThread caller,String callingPackage,Intent intent,String resolvedType,IBinder resultTo,String resultWho,int requestCode,int startFlags,ProfilerInfo profilerInfo,Bundle options,int userId){
enforceNotIsolatedCaller("startActivity");
userId = handleIncomingUser(Binder.getCallingPid(),Binder.getCallingUid(),userId,false,ALLOW_FULL_ONLY,"startActivity",null);
return mStackSupervisor.startActivityMayWait(caller,-1,callingPackage,intent,resolvedType,null,null,resultTo,resultWho,requestCode,startFlags,profilerInfo,null,null,options,userId,null,null);
}
可以看出,Activity的啓動過程又轉移到了ActivityStackSupervisor的startActivityMayWait方法中了,在startActivityMayWait中又調用了startActivityLocked方法,然後startActivityLocked方法有調用了startActivityUncheckedLocked方法,接着startActivityUncheckedLocked又調用了ActivityStack的resumeTopActivitiesLocked方法,這個時候啓動過程已經從ActivityStackSupervisor轉移到ActivityStack。
ActivityStack的resumeTopActivitiesLocked方法如下所示:
final boolean resumeTopActivityLocked(ActivityRecord prev,Bundle options){
if(inResumeTopActivity){
return false;
}
boolean result = false;
try{
inResumeTopActivity = true;
result = resumeTopActivityInnerLocked(prev,options);
}finally{
inResumeTopActivity = false;
}
return result;
}
從上面代碼可以看出,resumeTopActivityLocked調用了resumeTopActivityInnerLocked方法,resumeTopActivityInnerLocked又調用了ActivityStackSupervisor的startSpecificActivityLoaked方法,startSpecificActivityLoaked的源碼如下所示:
void startSpecificActivityLocked(ActivityRecord r,boolean andResume,boolean checkConfig){
ProcessRecord app = mService.getProcessRecordLocked(r.processName,r.info.applicationInfo.uid,true);
t.task.stack.setLaunchTime(r);
if(app != null && app.thread != null){
try{
if((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS)==0 || !"android".equals(r.info.packageName)){
app.addPackage(r.info.packageName,r.info.applicationInfo.versionCode,mService.mProcessStats);
}
realStartActivityLocked(r,app,andResume,checkConfig);
return;
}catch(RemoteException e){}
}
mService.startProcessLocked(r.proessName,r.info.applicationInfo,true,0,"activity",r.intent.getComponent(),false,false,true);
}