日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Activity的启动流程

發(fā)布時(shí)間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Activity的启动流程 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Activity的啟動(dòng)流程


努力工作

自己平時(shí)工作接觸的frameworks代碼比較多,但真正理解的很有限,一直在努力分析。。我主要還是用補(bǔ)丁的形式來看


core/java/android/app/Activity.java | 6 +++core/java/android/app/ActivityManagerNative.java | 14 ++++-core/java/android/app/ActivityThread.java | 63 +++++++++++++++++++++-core/java/android/app/ApplicationThreadNative.java | 7 +++core/java/android/app/IApplicationThread.java | 8 +++core/java/android/app/Instrumentation.java | 16 ++++++.../android/server/am/ActivityManagerService.java | 10 +++-.../java/com/android/server/am/ActivityStack.java | 12 ++++-.../android/server/am/ActivityStackSupervisor.java | 51 +++++++++++++++++-9 files changed, 181 insertions(+), 6 deletions(-)diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index 4b705dd..527cc38 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -3731,6 +3731,12 @@ public class Activity extends ContextThemeWrapper** @see #startActivity*/ + + /** + *Created by Smaster / one; + * mParent代表的是ActivityGroup, ActivityGroup最開始被用來在一個(gè)界面中嵌入多個(gè)Activity. + * --> Instrumentation execStartActivity()方法 + * */public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {if (mParent == null) {Instrumentation.ActivityResult ar = diff --git a/core/java/android/app/ActivityManagerNative.java b/core/java/android/app/ActivityManagerNative.java index 4e2ff0b..6ca7a32 100644 --- a/core/java/android/app/ActivityManagerNative.java +++ b/core/java/android/app/ActivityManagerNative.java @@ -78,6 +78,11 @@ public abstract class ActivityManagerNative extends Binder implements IActivityM/*** Retrieve the system's default/global activity manager.*/ + /** + * created by Smaster / three; + * + * + * */static public IActivityManager getDefault() {return gDefault.get();} @@ -2337,7 +2342,14 @@ public abstract class ActivityManagerNative extends Binder implements IActivityMpublic IBinder asBinder() {return this;} - + + /** + *created by Smaster / four; + * 單利封裝,&emsp;在后續(xù)的調(diào)用中直接返回之前創(chuàng)建的對象。 + * + * ----> AMS startActivity. + * + * */private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {protected IActivityManager create() {IBinder b = ServiceManager.getService("activity"); diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index dd49009..6ef3729 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -545,7 +545,21 @@ public final class ActivityThread {}private native void dumpGraphicsInfo(FileDescriptor fd); + + /** + * created by Smaster / nineteen; + * 繼承ApplicationThreadNative, ApplicationThreadNative 繼承Binder ; + * 并實(shí)現(xiàn)IApplicationThread接口, + * 類似AIDL + * ApplicationThreadNative 中&emsp;--> ApplicationThreadProxy類。 + * */+ /** + * created by Smaster / twenty-one; + * 再次從ApplicationThreadNative中歸來; + *&emsp;ApplicationThread 是真正實(shí)現(xiàn)IApplicationThread的類; + * --> scheduleLaunchActivity --> 啟動(dòng)Activity. + * */private class ApplicationThread extends ApplicationThreadNative {private static final String ONE_COUNT_COLUMN = "%21s %8d";private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d"; @@ -603,6 +617,9 @@ public final class ActivityThread {// we use token to identify this activity without having to send the// activity itself back to the activity manager. (matters more with ipc) + // + // created by Smaster / twenty-two; + // sendMessage --> 發(fā)送一個(gè)消息給H處理。 --> Handler&emsp;中&emsp;Hpublic final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,IVoiceInteractor voiceInteractor, int procState, Bundle state, @@ -1158,6 +1175,11 @@ public final class ActivityThread {}}+ /** + * created by Smaster / twenty-three; + * 對消息的處理;&emsp;LAUNCH_ACTIVITY ; + * + * */private class H extends Handler {public static final int LAUNCH_ACTIVITY = 100;public static final int PAUSE_ACTIVITY = 101; @@ -1266,6 +1288,12 @@ public final class ActivityThread {}return Integer.toString(code);} + + /** + * created by Smaster / twenty-four; + * handleMessage(r, null)方法實(shí)現(xiàn); + * + * */public void handleMessage(Message msg) {if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));switch (msg.what) { @@ -2172,9 +2200,20 @@ public final class ActivityThread {sendMessage(H.CLEAN_UP_CONTEXT, cci);}+ /** + * created by Smaster / twenty-six; + * 該方法最終完成activity的啓動(dòng); + * 主要完成六件事; + * + * . + * + * */private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {// System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")"); - + /** + * + * 1.從ActivityClientRecord中獲取待啟動(dòng)的Activity的組件信息; + * */ActivityInfo aInfo = r.activityInfo;if (r.packageInfo == null) {r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo, @@ -2195,6 +2234,10 @@ public final class ActivityThread {Activity activity = null;try { + /** + * 2. 通過Instrumention的newActivity方法使用類加載器創(chuàng)建Activity對象; + * + * */java.lang.ClassLoader cl = r.packageInfo.getClassLoader();activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent); @@ -2213,6 +2256,11 @@ public final class ActivityThread {}try { + + /** + * 3. 創(chuàng)建Application 對象; + * + * */Application app = r.packageInfo.makeApplication(false, mInstrumentation);if (localLOGV) Slog.v(TAG, "Performing launch of " + r); @@ -2224,6 +2272,12 @@ public final class ActivityThread {+ ", dir=" + r.packageInfo.getAppDir());if (activity != null) { + /** + * 4. 創(chuàng)建ContextImpl 對象并通過Activity的attach方法來完成一些重要數(shù)據(jù)的初始化; + * &emsp;Context的邏輯都是由ContexImpl來完成的。 + * &emsp;ContextImpl是通過activity的attach方法來和activity建立關(guān)聯(lián)。 + * &emsp; + * */Context appContext = createBaseContextForActivity(r, activity);CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());Configuration config = new Configuration(mCompatConfiguration); @@ -2248,6 +2302,9 @@ public final class ActivityThread {if (r.isPersistable()) {mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);} else { + /** + * 5. finished;i成 + * */mInstrumentation.callActivityOnCreate(activity, r.state);}if (!activity.mCalled) { @@ -2340,6 +2397,10 @@ public final class ActivityThread {return baseContext;}+ /** + * created by Smaster / twenty-five; + * ---> performLaunchActivity + * */private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {// If we are getting ready to gc after going to the background, well// we are back active so skip it. diff --git a/core/java/android/app/ApplicationThreadNative.java b/core/java/android/app/ApplicationThreadNative.java index 0123e16..544c00d 100644 --- a/core/java/android/app/ApplicationThreadNative.java +++ b/core/java/android/app/ApplicationThreadNative.java @@ -678,6 +678,13 @@ public abstract class ApplicationThreadNative extends Binder}}+/** + * created by Smaster / twenty; + * 系統(tǒng)AIDL / + * ApplicationThreadNative 是 IApplicationThread&emsp;的實(shí)現(xiàn)者。 + * 由于ApplicationThreadNative被定義為抽象類,&emsp;所以ApplicationThread就成 + * 了IApplicationThread的最終實(shí)現(xiàn)者。 + * */class ApplicationThreadProxy implements IApplicationThread {private final IBinder mRemote;diff --git a/core/java/android/app/IApplicationThread.java b/core/java/android/app/IApplicationThread.java index f53075c..226fe31 100644 --- a/core/java/android/app/IApplicationThread.java +++ b/core/java/android/app/IApplicationThread.java @@ -47,6 +47,14 @@ import java.util.Map;** {@hide}*/ + +/** + * created by Smaster / eighteen; + * 繼承IInterface接口,所以它是&emsp;Binder 類型的接口。 + *&emsp;包含大量啟動(dòng)&emsp;/ 停止&emsp;/ Activity的接口。&emsp;和&emsp;停止&emsp;/ 啟動(dòng)&emsp;服務(wù)的接口; + + * IApplicationThread 中的實(shí)現(xiàn)&emsp;---> &emsp;ActivityThread中內(nèi)部類&emsp;ApplicationThread. + * */public interface IApplicationThread extends IInterface {void schedulePauseActivity(IBinder token, boolean finished, boolean userLeaving,int configChanges, boolean dontReport) throws RemoteException; diff --git a/core/java/android/app/Instrumentation.java b/core/java/android/app/Instrumentation.java index 60a013e..228f982 100644 --- a/core/java/android/app/Instrumentation.java +++ b/core/java/android/app/Instrumentation.java @@ -1482,6 +1482,10 @@ public class Instrumentation {intent.resolveTypeIfNeeded(who.getContentResolver()),token, target != null ? target.mEmbeddedID : null,requestCode, 0, null, options); + /** + * created by Smaster / five; + * 檢查activity的啟動(dòng)結(jié)果 + * */checkStartActivityResult(result, intent);} catch (RemoteException e) {} @@ -1632,6 +1636,13 @@ public class Instrumentation {** {@hide}*/ + /** + * created by Smaster / two; + * ---> ActivityManagerNative.getDefault().startActivityAsUser(); + * AMS中實(shí)現(xiàn); + *&emsp;AMS 繼承&emsp;AMN , AMN繼承Binder 并實(shí)現(xiàn)IActivityManager. + * 因此,AMS也是一個(gè)Binder, 它是IActivityManager的具體實(shí)現(xiàn)。 + * */public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target,Intent intent, int requestCode, Bundle options, UserHandle user) { @@ -1749,6 +1760,11 @@ public class Instrumentation {}/** @hide */ + /** + * creatd by Smaster / six; + * 當(dāng)無法正確啟動(dòng)一個(gè)Activity時(shí),這個(gè)方法會(huì)拋出一個(gè)異常信息; + *&emsp;當(dāng)待啟動(dòng)的Activity沒有在AndroidManifest中注冊時(shí),&emsp;會(huì)拋出這個(gè)異常; + * */public static void checkStartActivityResult(int res, Object intent) {if (res >= ActivityManager.START_SUCCESS) {return; diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 8dfb321..a20d5c3 100755 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -3535,7 +3535,11 @@ public final class ActivityManagerService extends ActivityManagerNative}mProcessObservers.finishBroadcast();} - + + /** + * created by Smaster / seven; + * + * */@Overridepublic final int startActivity(IApplicationThread caller, String callingPackage,Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, @@ -3545,6 +3549,10 @@ public final class ActivityManagerService extends ActivityManagerNativeUserHandle.getCallingUserId());}+ /** + * creatd by Smaster / eight; + * --> ActivityStackSupervisor startactivityMayWait方法; + * */@Overridepublic final int startActivityAsUser(IApplicationThread caller, String callingPackage,Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java index e1b8278..dbe3249 100755 --- a/services/core/java/com/android/server/am/ActivityStack.java +++ b/services/core/java/com/android/server/am/ActivityStack.java @@ -1467,6 +1467,11 @@ final class ActivityStack {return resumeTopActivityLocked(prev, null);}+ /** + * created by Smaster / thirteen; + * --> resumeTopActivityInnerLocked + * + * */final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {if (inResumeTopActivity) {// Don't even start recursing. @@ -1483,7 +1488,12 @@ final class ActivityStack {}return result;} - + + /** + * creatd by Smaster / fourteen; + * --> ActivityStackSupervisor startSpecificActivityLocked方法 + * + * */final boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java index 03dd3c0..e3c7ecb 100644 --- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java +++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java @@ -810,6 +810,11 @@ public final class ActivityStackSupervisor implements DisplayListener {0, 0, 0, null, false, null, null, null);}+ /** + * created by Smaster / nine; + * --> startActivityLocked; + * */ +final int startActivityMayWait(IApplicationThread caller, int callingUid,String callingPackage, Intent intent, String resolvedType,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, @@ -1058,6 +1063,10 @@ public final class ActivityStackSupervisor implements DisplayListener {return ActivityManager.START_SUCCESS;}+ /** + * created by Smaster / sixteen; + * + * */final boolean realStartActivityLocked(ActivityRecord r,ProcessRecord app, boolean andResume, boolean checkConfig)throws RemoteException { @@ -1154,6 +1163,25 @@ public final class ActivityStackSupervisor implements DisplayListener {? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,mService.mAutoStopProfiler) : null;app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP); + /* created by Smaster / seventeen; + *&emsp;總結(jié): + * startActivityMayWait + * | + * startActivityLocked + * | + * startActivityUncheckedLocked ---> resumeTopActivitiesLocked + * | + * startSpecificActivityLocked <--- resumeTopActivitiesLocked + * | + * realStartActivityLocked ActivityStack; + * + * ActivityStackSupervisor; + * + * + * + * app.thread的類型為IApplicationThread + * + * **/app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),r.compat, r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, @@ -1235,6 +1263,10 @@ public final class ActivityStackSupervisor implements DisplayListener {return true;}+ /** + * created by Smaster / fifteen; + * -- > realStartActivityLocked; + * */void startSpecificActivityLocked(ActivityRecord r,boolean andResume, boolean checkConfig) {// Is this activity's application already running? @@ -1269,6 +1301,11 @@ public final class ActivityStackSupervisor implements DisplayListener {"activity", r.intent.getComponent(), false, false, true);}+ /** + * created by Smaster / ten ; + * --> startActivityUncheckedLocked方法; + * + * */final int startActivityLocked(IApplicationThread caller,Intent intent, String resolvedType, ActivityInfo aInfo,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, @@ -1570,7 +1607,12 @@ public final class ActivityStackSupervisor implements DisplayListener {moveHomeStack(isHomeActivity);}} - +&emsp;&emsp;/** + *created by Smaster / eleven; + * + * ---> resumeTopActivitiesLocked(); + * + */&emsp;final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,boolean doResume, Bundle options, TaskRecord inTask) { @@ -2417,7 +2459,12 @@ public final class ActivityStackSupervisor implements DisplayListener {boolean resumeTopActivitiesLocked() {return resumeTopActivitiesLocked(null, null, null);} - + + /** + * created by Smaster / twelve; + * ---> ActivityStack&emsp;resumeTopActivityLocked; + * + * */boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,Bundle targetOptions) {if (targetStack == null) { -- 復(fù)制代碼

我會(huì)逐漸增加注釋

轉(zhuǎn)載于:https://juejin.im/post/5b00315d51882542ad7743a2

總結(jié)

以上是生活随笔為你收集整理的Activity的启动流程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。