Android App啟動(dòng)流程

拋出問(wèn)題:
1. Android系統(tǒng)桌面是什么
2. 點(diǎn)擊應(yīng)用圖標(biāo)后Android系統(tǒng)執(zhí)行了什么操作

用文字總結(jié)App啟動(dòng)流程可以分為以下步驟:

1. Launcher通過(guò)Binder建立Launcher所在進(jìn)程與system_server進(jìn)程(ActivityManagerService所在進(jìn)程)的通信,通知ActivityManagerService即將要啟動(dòng)一個(gè)Activity
2. ActivityManagerService通過(guò)Binder讓Launcher進(jìn)入pause狀態(tài)
3. Launcher進(jìn)入pause狀態(tài)后,通過(guò)Binder告知ActivityManagerService,隨后ActivityManagerService創(chuàng)建一個(gè)進(jìn)程(將要打開(kāi)的應(yīng)用進(jìn)程)并啟動(dòng)ActivityThread(應(yīng)用的UI線程)
4. ActivityThread通過(guò)Binder將ApplicationThread類型的Binder對(duì)象傳遞給ActivityManagerService,方便ActivityManagerService后續(xù)與其的通信
5. 準(zhǔn)備工作完成后,ActivityManagerService通知ActivityThread啟動(dòng)Activity
6. ActivityThread調(diào)度執(zhí)行Activity的生命周期方法,完成啟動(dòng)Activity的工作

Activity是視圖存在的根本,那么我們可以通過(guò)命令adb shell dumpsys activity activities判斷是哪個(gè)Activity為我們呈現(xiàn)桌面視圖的

點(diǎn)擊應(yīng)用圖標(biāo)后Android系統(tǒng)執(zhí)行了什么操作

呈現(xiàn)Android桌面視圖(View)->點(diǎn)擊View上某個(gè)應(yīng)用圖標(biāo)->產(chǎn)生點(diǎn)擊事件->點(diǎn)擊事件被響應(yīng)->通知Android系統(tǒng)的某個(gè)/某些進(jìn)程->Android系統(tǒng)執(zhí)行某些操作->啟動(dòng)App

Launcher如何響應(yīng)由我們產(chǎn)生的點(diǎn)擊事件

產(chǎn)生點(diǎn)擊事件后,如果產(chǎn)生點(diǎn)擊事件的View的Tag是ShortcutInfo(即啟動(dòng)應(yīng)用的快捷方式),就會(huì)取得ShortcutInfo中保存的Intent(這個(gè)Intent指向我們要啟動(dòng)的App),然后執(zhí)行startActivitySafely(v, intent, tag)方法,而startActivitySafely方法只是對(duì)startActivity方法的簡(jiǎn)單封裝。

所以,Launcher響應(yīng)我們產(chǎn)生的點(diǎn)擊事件后,實(shí)際上就是啟動(dòng)一個(gè)新的Activity。請(qǐng)看代碼:

/**
* Launches the intent referred by the clicked shortcut.
*
* @param v The view representing the clicked shortcut.
*/
public void onClick(View v) {
// Make sure that rogue clicks don't get through while allapps is launching, or after the
// view has detached (it's possible for this to happen if the view is removed mid touch).
if (v.getWindowToken() == null) {
return;
}

if (!mWorkspace.isFinishedSwitchingState()) {
return;
}

Object tag = v.getTag();
if (tag instanceof ShortcutInfo) {
// Open shortcut
final Intent intent = ((ShortcutInfo) tag).intent;
int[] pos = new int[2];
v.getLocationOnScreen(pos);
intent.setSourceBounds(new Rect(pos[0], pos[1],
pos[0] + v.getWidth(), pos[1] + v.getHeight()));

boolean success = startActivitySafely(v, intent, tag);

if (success && v instanceof BubbleTextView) {
mWaitingForResume = (BubbleTextView) v;
mWaitingForResume.setStayPressed(true);
}
} else if (tag instanceof FolderInfo) {
if (v instanceof FolderIcon) {
FolderIcon fi = (FolderIcon) v;
handleFolderClick(fi);
}
} else if (v == mAllAppsButton) {
if (isAllAppsVisible()) {
showWorkspace(true);
} else {
onClickAllAppsButton(v);
}
}
}

boolean startActivitySafely(View v, Intent intent, Object tag) {
boolean success = false;
try {
success = startActivity(v, intent, tag);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
}
return success;
}

現(xiàn)在回想下App開(kāi)發(fā)時(shí),每個(gè)App都需要有一個(gè)“MainActivity”,這個(gè)Activity必須在AndroidManifest.xml文件中有以下配置:

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

在配置AndroidManifest.xml文件時(shí),將Activity的Action指定為android.intent.action.MAIN,會(huì)使Activity在一個(gè)新的Task中啟動(dòng)(Task是一個(gè)Activity棧)。將category指定為android.intent.category.LAUNCHER,表示通過(guò)Intent啟動(dòng)此Activity時(shí),只接受category為L(zhǎng)AUNCHER的Intent。

所以,Launcher將會(huì)通過(guò)App的快捷方式(ShortcutInfo)得到應(yīng)用的Intent,并通過(guò)這個(gè)Intent啟動(dòng)應(yīng)用的“MainActivity”,從而啟動(dòng)應(yīng)用。

所以我們研究的問(wèn)題就從“App啟動(dòng)流程”變?yōu)椤癆ctivity啟動(dòng)流程”。

Launcher通過(guò)Binder通知ActivityManagerService啟動(dòng)Activity

,將Intent的Flag設(shè)為Intent.FLAG_ACTIVITY_NEW_TASK,使得Android系統(tǒng)將創(chuàng)建一個(gè)新的Task來(lái)放置即將被打開(kāi)的新Activity(應(yīng)用的“MainActivity)。然后獲取一個(gè)布爾值以用于后續(xù)判斷是否顯示啟動(dòng)App的動(dòng)畫(huà)。

然后獲取Intent中是否傳輸了Parcelable格式的用戶句柄,并通過(guò)Context.LAUNCHER_APPS_SERVICE獲取用于在多用戶情境下啟動(dòng)App的系統(tǒng)服務(wù)。

不管是否顯示啟動(dòng)App的動(dòng)畫(huà),最終都會(huì)執(zhí)行startActivity(intent)launcherApps.startMainActivity方法以啟動(dòng)應(yīng)用的“MainActivity”。

代碼如下:boolean startActivity(View v, Intent intent, Object tag) {

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {
// Only launch using the new animation if the shortcut has not opted out (this is a
// private contract between launcher and may be ignored in the future).
boolean useLaunchAnimation = (v != null) &&
!intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
UserHandle user = (UserHandle) intent.getParcelableExtra(ApplicationInfo.EXTRA_PROFILE);
LauncherApps launcherApps = (LauncherApps)
this.getSystemService(Context.LAUNCHER_APPS_SERVICE);
if (useLaunchAnimation) {
ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
v.getMeasuredWidth(), v.getMeasuredHeight());
if (user == null || user.equals(android.os.Process.myUserHandle())) {
// Could be launching some bookkeeping activity
startActivity(intent, opts.toBundle());
} else {
launcherApps.startMainActivity(intent.getComponent(), user,
intent.getSourceBounds(),
opts.toBundle());
}
} else {
if (user == null || user.equals(android.os.Process.myUserHandle())) {
startActivity(intent);
} else {
launcherApps.startMainActivity(intent.getComponent(), user,
intent.getSourceBounds(), null);
}
}
return true;
} catch (SecurityException e) {
Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Log.e(TAG, "Launcher does not have the permission to launch " + intent +
". Make sure to create a MAIN intent-filter for the corresponding activity " +
"or use the exported attribute for this activity. "
+ "tag="+ tag + " intent=" + intent, e);
}
return false;
}

launcherApps.startMainActivity只在用戶句柄不為空且用戶句柄不等于當(dāng)前進(jìn)程句柄(handle)時(shí)(其他用戶的句柄)調(diào)用,

為什么用戶句柄會(huì)影響Activity的啟動(dòng)方式?

我們需要取得用戶A的句柄(和用戶A相關(guān)的數(shù)據(jù)),將我們想啟動(dòng)的用戶B的App的Intent、用戶A的句柄交給內(nèi)核,讓擁有權(quán)限的

startActivity(intent)如何啟動(dòng)Activity

進(jìn)入Activity類后層層深入就可以看到最終調(diào)用的是startActivityForResult方法:

從代碼上看,如果Launcher有mParent Activity,就會(huì)執(zhí)行mParent.startActivityFromChild;如果沒(méi)有,就會(huì)執(zhí)行mInstrumentation.execStartActivity。進(jìn)入mParent.startActivityFromChild方法會(huì)看到最終也是執(zhí)行了mInstrumentation.execStartActivity。執(zhí)行完成后,會(huì)取得一個(gè)ActivityResult對(duì)象,用于給調(diào)用者Activity傳遞一些數(shù)據(jù),最后在Activity切換時(shí)顯示Transition動(dòng)畫(huà)。

這里有一點(diǎn)需要指出的是:這里的ParentActivity指的是類似TabActivity、ActivityGroup關(guān)系的嵌套Activity。之所以要強(qiáng)調(diào)parent和child,是要避免混亂的Activity嵌套關(guān)系。代碼如下:

public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
if (mParent == null) {
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);
}
}
}

我們進(jìn)入Instrumentation(管家婆類)類看看execStartActivity方法吧:

我們通過(guò)參數(shù)IBinder contextThread取得一個(gè)IApplicationThread類型的對(duì)象whoThread,而contextThread是由mMainThread.getApplicationThread()取得的ApplicationThread對(duì)象,此時(shí)mMainThread指的就是Launcher應(yīng)用的主線程,所以whoThread指代的自然是Launcher的ApplicationThread。

因?yàn)锳ctivity的onProvideReferrer()方法默認(rèn)返回null,除非該方法被重寫(xiě),而我們使用的Launcher并沒(méi)有重寫(xiě)該方法,所以不用管referrer。

然后判斷是否有ActivityMonitor,如果有,則即將要打開(kāi)的Activity是否和ActivityMonitor中保存的IntentFilter匹配,如果匹配則增加ActivityMonitor的計(jì)數(shù)。大致是用于監(jiān)控符合匹配規(guī)則的Activity的數(shù)量的。

最后調(diào)用ActivityManagerNative.getDefault().startActivity(whoThread, who.getBasePackageName(), intent, intent.resolveTypeIfNeeded(who.getContentResolver()), token, target != null ? target.mEmbeddedID : null, requestCode, 0, null, options);啟動(dòng)Activity,并檢查啟動(dòng)是否成功。換句話說(shuō),最終負(fù)責(zé)啟動(dòng)Activity的是ActivityManager,前面得到的ApplicationThread也是在這里使用的。代碼:

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();
int result = ActivityManagerNative.getDefault()
.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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容