Android Activity的啟動過程過程分析
前言
在了解Activity的啟動的過程的時候,我們需要先了解下面幾個東西是什么,這將更加有助于你深入了解android 活動的啟動機制
老司機繞道
Activity
這個類大家再熟悉不過了,android最重要的組件之一,實際上它就是一個Java對象,它會被創建,同時也會被垃圾回收就只銷毀,只不過它受AMS(ActivityManagerService)管理,所以它才有生命周期。ActivityResult
Activity 管理服務端中,Activity的記錄和緩存,換句話說:就是客戶端啟動一個Activity,AMS(ActivityManagerService)會對它進行緩存,而緩存的Activity的類型就是ActivityResult。AMS - ActivityManagerService
android系統服務,Android管理的服務端,用于管理Activity的各種行為,控制Activity的生命周期,派發消息事件,低內存管理等等。實現了IBinder接口,可以用于進程間的通信。ApplicationThread
該類實現了IBinder接口,Activity整個框架中客戶端和服務端AMS(ActivityManagerService)通信的接口。同時也是ActivityThread的內部類。這樣就有效的把AMS(ActivityManagerService)和ActivityThread綁在了一起。ActivityThread
ApplicationThread所綁定的客戶端就是ActivityThread,ActivityThread這個類在Activity客戶端中的作用舉足輕重。
- 它是應用程序的入口,大家都知道,Java程序的入口為main()方法,同樣,當AMS(ActivityManagerService)拉起了一個新的進程,同時啟動一個主線程的時候,主線程就從ActivityThread.main方法開始執行,他回初始化一些對象,然后自己進入消息等待隊列,也就是Looper.looper();一旦進入loop()方法,線程就進入了死循環,再也不會退出;一直等待別人給它發消息,然后執行這個消息。這也是EDT(事件驅動模型)的原理。
- 它是Activity客戶端的管理類,由它來決定什么時候調用onCreate(),什么時候調用onResume()方法,當Activity發起一個請求時,比如startActivity()或者finish()的時候,它就會來除了這個請求,然后調用其它的人來做具體的事。
Launcher
Android系統啟動后,加載的第一個程序,是其他應用程序的入口。
也可以簡單的理解為我們看到的android桌面Instrumentation
這個類在Activity啟動的時候以及廣播注冊的時候都會調用,除了跟android的測試有關之外,還是Activity管理中實際做事的人,比如說startActivity(),在某種情況下,就是調用這個類,然后調用AMS(ActivityManagerService),但是有的時候也是通過ApplicationThread去訪問AMS的。PackageManagerService
是Android系統中最常用的服務之一。它負責系統中Package的管理,應用程序的安裝、卸載、信息查詢等。這只是一個很籠統的說法。
PackageManagerService詳解點我
正文
Activity作為android 四大組件之一,也是最基本的組件,負責用戶的交互的所有功能。Activity的啟動過程也并非是一件神秘的事情,接下來就簡單的從源碼的角度分析一下Activity的啟動過程。
根Activity一般就是指我們項目中的MainActivity,代表了一個android應用程序,一般也是在一個新的進程中啟動起來。在android系統中所有的Activity組件都保存在堆棧中,我們啟動一個新的Activity組件就位于上一個Activity的上面,那么我們從桌面(Launcher)打開一個APP的 是一個怎么樣的過程呢,如下所示:
1. Launcher向ActivityManagerService發送一個啟動MainActivity的請求;
2. ActivityManagerService首先將MainActivity的相關信息保存下來,然后向Launcher發送一個實質進入暫停的狀態的請求;
3. Launcher收到暫停狀態之后,就會向ActivityManagerService發送一個已經進入暫停的狀態的請求,便于ActivityManagerService繼續執行啟動MainActivity的操作;
4. ActivityManagerService檢查用于運行MainActivity的進程,如果不存在,則會啟動一個新的進程
5. ActivityManagerService將第(2)步保存下來的MainActivity相關信息發送個新的進程,便于該進程啟動MainActivity組件
-
Launcher.startActivitySafely
boolean startActivitySafely(Intent intent, Object tag) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { startActivity(intent); return true; } catch (ActivityNotFoundException e) {} }
當我們在Launcher上點擊應用圖標時,startActivitySafely方法會被調用。需要啟動的Activity信息保存在Intent中,包括action、category等等。那么Launcher是如何獲得Intent里面的這些信息呢?首先,系統在啟動時會啟動一個叫做PackageManagerService的管理服務,并且通過它來安裝系統中的應用程序,這個過程中,PackageManagerService會對應用程序的配置文件androidManifest.xml進行解析,從而得到程序里面的組件信息(包括Activity、Service、Broadcast等),然后PackageManagerService去查詢所有的action為
android.intent.action.MAIN
并且category為“android.intent.category.LAUNCHER”的Activity,然后為每一個應用程序創建一個快捷圖標,并把程序信息與之關聯。上述代碼中,Activity的啟動標志位位置為Intent.FLAG_ACTIVITY_NEW_TASK
,便于它可以在一個新的任務戰中啟動。 -
Activity.startActivity
@Override public void startActivity(Intent intent, @Nullable Bundle options) { if (options != null) { startActivityForResult(intent, -1, options); } else { // Note we want to go through this call for compatibility with // applications that may have overridden the method. startActivityForResult(intent, -1); } }
調用startActivityForResult,第二個參數requestCode為-1,則表示Activity關閉時不需要將結果傳回來。
-
Activity.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); } } }
實際上是調用的
mInstrumentation.execStartActivity
來啟動Activity,mInstrumentation類型為Instrumentation, Instrumentation用于監控程序和系統之間的交互操作。mInstrumentation代為執行Activity的啟動操作,便于它可以監控這個交互過程。mMainThread的類型為ActivityThread,用于描述一個應用程序進程,系統每啟動一個程序都會在它里面加載一個ActivityThread的實例,并且將實例保存在Activity的成員變量mMainThread中,而mMainThread.getApplicationThread()則用于獲取內部為一個ApplicationThread的本地Binder對象。mToken的類型為IBinder,它是一個Binder的代理對象,指向了ActivityManagerService中一個類型為ActivityResult的本地對象。每一個已經啟動的Activity在ActivityManagerService中都有一個對應的ActivityRecord對象,用于維護Activity的運行狀態以及信息。 -
Instrumentation.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);
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 = 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;
}
```
> ActivityManagerNative.getDefault()獲取一個ActivityManagerService的代理對象,然后調用它的startActivity方法來通知ActivityManagerService去啟動Activity。中間還有一系列的過程,最后是調用ActivityThread中的私有內部類ApplicationThread的scheduleLauncherActivity來進行Activity的啟動。
-
AcivityThread.ApplicationThread.scheduleLaunchActivity
@Override
public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
int procState, Bundle state, PersistableBundle persistentState,
List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
updateProcessState(procState, false);
ActivityClientRecord r = new ActivityClientRecord();
r.token = token;
r.ident = ident;
r.intent = intent;
r.referrer = referrer;
r.voiceInteractor = voiceInteractor;
r.activityInfo = info;
r.compatInfo = compatInfo;
r.state = state;
r.persistentState = persistentState;
r.pendingResults = pendingResults;
r.pendingIntents = pendingNewIntents;
r.startsNotResumed = notResumed;
r.isForward = isForward;
r.profilerInfo = profilerInfo;
r.overrideConfig = overrideConfig;
updatePendingConfiguration(curConfig);
sendMessage(H.LAUNCH_ACTIVITY, r);
}
```
> 構造了一個ActivityClientRecord,然后調用sendMessage發送一個消息。在引用程序對應的進程中。每一個Activity組件都是使用一個ActivityClientRecord對象來描述的,他們保存在ActivityThread類的成員變量mActivities中。
-
ActivityThread.H.handleMessage
public void handleMessage(Message msg) { if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what)); switch (msg.what) { case LAUNCH_ACTIVITY: { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart"); final ActivityClientRecord r = (ActivityClientRecord) msg.obj; r.packageInfo = getPackageInfoNoCheck( r.activityInfo.applicationInfo, r.compatInfo); handleLaunchActivity(r, null, "LAUNCH_ACTIVITY"); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); } break; case RELAUNCH_ACTIVITY: { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart"); ActivityClientRecord r = (ActivityClientRecord)msg.obj; handleRelaunchActivity(r); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); } break; case PAUSE_ACTIVITY: { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause"); SomeArgs args = (SomeArgs) msg.obj; handlePauseActivity((IBinder) args.arg1, false, (args.argi1 & USER_LEAVING) != 0, args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3); maybeSnapshot(); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); } break; // ………… 此處有代碼省略
}
> 首先將msg里面的obj轉化成一個ActivityClientRecord對象,然后調用來獲取一個LoaderAPK對象并保存在ActivityClientRecord對象的成員變量packageInfo中。loader對象用于藐視一個已經加載的APK文件。最后調用handleLauncherActivity來啟動Activity組件。
7. ActivityThread.handleLaunchActivity
```java
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
// If we are getting ready to gc after going to the background, well
// we are back active so skip it.
unscheduleGcIdler();
mSomeActivitiesChanged = true;
if (r.profilerInfo != null) {
mProfiler.setProfiler(r.profilerInfo);
mProfiler.startProfiling();
}
// Make sure we are running with the most recent config.
handleConfigurationChanged(null, null);
if (localLOGV) Slog.v(
TAG, "Handling launch of " + r);
// Initialize before creating the activity
WindowManagerGlobal.initialize();
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
r.createdConfig = new Configuration(mConfiguration);
reportSizeConfigurations(r);
Bundle oldState = r.state;
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
if (!r.activity.mFinished && r.startsNotResumed) {
// The activity manager actually wants this one to start out paused, because it
// needs to be visible but isn't in the foreground. We accomplish this by going
// through the normal startup (because activities expect to go through onResume()
// the first time they run, before their window is displayed), and then pausing it.
// However, in this case we do -not- need to do the full pause cycle (of freezing
// and such) because the activity manager assumes it can just retain the current
// state it has.
performPauseActivityIfNeeded(r, reason);
// We need to keep around the original state, in case we need to be created again.
// But we only do this for pre-Honeycomb apps, which always save their state when
// pausing, so we can not have them save their state when restarting from a paused
// state. For HC and later, we want to (and can) let the state be saved as the
// normal part of stopping the activity.
if (r.isPreHoneycomb()) {
r.state = oldState;
}
}
} else {
// If there was an error, for any reason, tell the activity manager to stop us.
try {
ActivityManagerNative.getDefault()
.finishActivity(r.token, Activity.RESULT_CANCELED, null,
Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
}
Activity a = performLaunchActivity(r ,customIntent);
真正完成Activity的吊起,Activity被實例化,onCreate被調用。performLaunchActivity函數加載用戶自定義的Activity的派生類,并執行其onCreate函數,它將返回此Activity對象。·``handleResumeActivity(r.token, false, r.isForward,!r.activity.mFinished && !r.startsNotResumed);```同理,再調用Activity實例的Resume(用戶界面可見)
-
ActivityThread. performLaunchActivity
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")"); ActivityInfo aInfo = r.activityInfo; if (r.packageInfo == null) { r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo, Context.CONTEXT_INCLUDE_CODE); } ComponentName component = r.intent.getComponent(); if (component == null) { component = r.intent.resolveActivity( mInitialApplication.getPackageManager()); r.intent.setComponent(component); } if (r.activityInfo.targetActivity != null) { component = new ComponentName(r.activityInfo.packageName, r.activityInfo.targetActivity); } Activity activity = null; try { java.lang.ClassLoader cl = r.packageInfo.getClassLoader(); activity = mInstrumentation.newActivity( cl, component.getClassName(), r.intent); StrictMode.incrementExpectedActivityCount(activity.getClass()); r.intent.setExtrasClassLoader(cl); r.intent.prepareToEnterProcess(); if (r.state != null) { r.state.setClassLoader(cl); } } catch (Exception e) { if (!mInstrumentation.onException(activity, e)) { throw new RuntimeException( "Unable to instantiate activity " + component + ": " + e.toString(), e); } } try { Application app = r.packageInfo.makeApplication(false, mInstrumentation); if (localLOGV) Slog.v(TAG, "Performing launch of " + r); if (localLOGV) Slog.v( TAG, r + ": app=" + app + ", appName=" + app.getPackageName() + ", pkg=" + r.packageInfo.getPackageName() + ", comp=" + r.intent.getComponent().toShortString() + ", dir=" + r.packageInfo.getAppDir()); if (activity != null) { Context appContext = createBaseContextForActivity(r, activity); CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager()); Configuration config = new Configuration(mCompatConfiguration); if (r.overrideConfig != null) { config.updateFrom(r.overrideConfig); } if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity " + r.activityInfo.name + " with config " + config); Window window = null; if (r.mPendingRemoveWindow != null && r.mPreserveWindow) { window = r.mPendingRemoveWindow; r.mPendingRemoveWindow = null; r.mPendingRemoveWindowManager = null; } activity.attach(appContext, this, getInstrumentation(), r.token, r.ident, app, r.intent, r.activityInfo, title, r.parent, r.embeddedID, r.lastNonConfigurationInstances, config, r.referrer, r.voiceInteractor, window); if (customIntent != null) { activity.mIntent = customIntent; } r.lastNonConfigurationInstances = null; activity.mStartedActivity = false; int theme = r.activityInfo.getThemeResource(); if (theme != 0) { activity.setTheme(theme); } activity.mCalled = false; if (r.isPersistable()) { mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState); } else { mInstrumentation.callActivityOnCreate(activity, r.state); } if (!activity.mCalled) { throw new SuperNotCalledException( "Activity " + r.intent.getComponent().toShortString() + " did not call through to super.onCreate()"); } r.activity = activity; r.stopped = true; if (!r.activity.mFinished) { activity.performStart(); r.stopped = false; } if (!r.activity.mFinished) { if (r.isPersistable()) { if (r.state != null || r.persistentState != null) { mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state, r.persistentState); } } else if (r.state != null) { mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state); } } if (!r.activity.mFinished) { activity.mCalled = false; if (r.isPersistable()) { mInstrumentation.callActivityOnPostCreate(activity, r.state, r.persistentState); } else { mInstrumentation.callActivityOnPostCreate(activity, r.state); } if (!activity.mCalled) { throw new SuperNotCalledException( "Activity " + r.intent.getComponent().toShortString() + " did not call through to super.onPostCreate()"); } } } r.paused = true; mActivities.put(r.token, r); } catch (SuperNotCalledException e) { throw e; } catch (Exception e) { if (!mInstrumentation.onException(activity, e)) { throw new RuntimeException( "Unable to start activity " + component + ": " + e.toString(), e); } } return activity; }
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent)
將Activity類文件加載到內存中,創建Activity實例mInstrumentation.callActivityOnCreate(activity, r.state);
至此,Activity啟動過程就結束了,其生命周期由ApplicationThread來管理;
ActivityRecord里面的token,是一個Binder的代理對象,和ActivityClientRecord對象一樣,都是用來藐視所啟動的Activity組件,只不過前者是在Activityma中使用,后者是在引用程序進程中使用。
到這里,Activity啟動過程就完了。
子Activity的啟動過程和應用程序的啟動過程是非常類似的,過程如下:
1. 已啟動的Activity向ActivityManagerService發送一個啟動ChildActivity的請求;
2. ActivityManagerService首先將ChildActivity的信息保存下來,再向Activity發送一個中止的請求;
3. Activity收到請求進入中止狀態,告訴ActivityManagerService,便于ActivityManagerService繼續執行啟動ChildActivity的操作。
4. ActivityManagerService檢查ChildActivity所運行的進程是否存在,存在就發送ChildActivity信息給它,以便進行啟動,
總結
經過了應用進程和系統進程一系列操作,完成Activity的啟動,并且也找到了Activity的生命周期方法到底是怎么調用的,只是很多方法太長,其中的一些邏輯還不是很理解,后面有時間再仔細看看