Android 源码分析 Activity 启动

Posted 小图包

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 源码分析 Activity 启动相关的知识,希望对你有一定的参考价值。

android  启动类的作用

Instrumentation     完成对Application和Activity初始化和生命周期调用的工具类。用来监控系统与应用的交互。
ActivityThread     管理应用进程的主线程的执行。
ApplicationThread     用来实现ActivityManagerService与ActivityThread之间的交互。在ActivityManagerService管理应用进程中的Activity时,通过ApplicationThread的代理对象与ActivityThread通讯。(继承IApplicationThread.Stub)
ActivityClientRecord     在应用程序进程中启动的每一个Activity组件都使用一个ActivityClientRecord对象来描述。并且保存在ActivityThread类的成员变量mActivities中。
ActivityRecord     在AMS进程中,一个ActivityRecord对应一个Activity,保存了一个Activity的所有信息。这些ActivityRecord对象对应于App进程中的ActivityClientRecord。
TaskRecord   任务栈,每一个TaskRecord都可能存在一个或多个ActivityRecord,栈顶的ActivityRecord表示当前可见的界面 。
ActivityStack    一个栈式管理结构,每一个ActivityStack都可能存在一个或多个TaskRecord,栈顶的TaskRecord表示当前可见的任务。
ActivityStackSupervisor     管理着多个ActivityStack,但当前只会有一个获取焦点(Focused)ActivityStack。 ProcessRecord     记录着属于一个进程的所有ActivityRecord,运行在不同TaskRecord中的ActivityRecord可能是属于同一个 ProcessRecord。
ActivityManagerService     AMS主要负责系统中四大组件的启动、切换、及进程的管理和调度等工作,Android8.0中AMS继承IActivityManager.Stub。

系统进程、系统桌面 Launcher 进程、应用程序进程的启动过程,那么接下来点击系统桌面图标就该进入到应用程序进程的根 Activity了,

应用程序根 Activity 启动过程

Activity 启动过程分为 2 种,一种是应用程序根 Activity 的启动也就是在 XML 布局中有该配置的属性,

<action android:name="android.intent.action.MAIN"/>

第二种就是我们程序中调用 startActivity 启动。不过这 2 种在最后源码中的调用几乎一样,可以这样说只要理解了第一种,那么第二种理解起来就轻松多了。下面我们先来分析第一种。

 一 Launcher 请求 AMS 过程

我们点击某个应用的快捷图标的时候,就会通过 Launcher 请求 AMS 来启动该应用程序。

Launcher 请求 AMS 的时序图如下:

我们点击应用图标的时候,就会调用 Launcher 的 startActivitySafely 函数

//Launcher.java
    public boolean startActivitySafely(View v, Intent intent, ItemInfo item) 
        if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) 
            Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
            return false;
        
       
         1. 为启动应用的 Activity 添加一个新的任务栈
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (v != null) 
            intent.setSourceBounds(getViewBounds(v));
        
        try 
            if (Utilities.ATLEAST_MARSHMALLOW
                    && (item instanceof ShortcutInfo)
                    && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
                     || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
                    && !((ShortcutInfo) item).isPromise()) 
                // Shortcuts need some special checks due to legacy reasons.
                startShortcutIntentSafely(intent, optsBundle, item);
             else if (user == null || user.equals(Process.myUserHandle())) 
                // Could be launching some bookkeeping activity
                2处调用 Activity 内部的函数 startActivity
                startActivity(intent, optsBundle);
             else 
                LauncherAppsCompat.getInstance(this).startActivityForProfile(
                        intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
            
            return true;
         catch (ActivityNotFoundException|SecurityException e) 
            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
            Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
        
        return false;
    

2 处调用 Activity 内部的函数 startActivity,详细代码如下:

//Activity.java
    @Override
    public void startActivity(Intent intent, @Nullable Bundle options) 
       
        if (options != null) 
        有传参行为,调用 Activity startActivityForResult 函数,将参数传递下去

            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);
        
    

在 Activity startActivity 函数中最终会调用 startActivityForResult 函数

//Activity.java
    public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
            @Nullable Bundle options) 
     
        if (mParent == null) 
            options = transferSpringboardActivityOptions(options);

          1
         Instrumentation:主要用来监控应用程序和系统的交互
         调用 Instrumentation 的 execStartActivity 函数。
            Instrumentation.ActivityResult ar =
                mInstrumentation.execStartActivity(
                    this, mMainThread.getApplicationThread(), mToken, this,
                    intent, requestCode, options);
            if (ar != null) 
               2  Activity onActivityResult 回调
                mMainThread.sendActivityResult(
                    mToken, mEmbeddedID, requestCode, ar.getResultCode(),
                    ar.getResultData());
            
          ...
         else 
            ...
        
    

我们看注释 1 的源码实现,如下:

//Instrumentation.java
    public ActivityResult execStartActivity(
            Context who, IBinder contextThread, IBinder token, Activity target,
            Intent intent, int requestCode, Bundle options) 
      	1 
        IApplicationThread whoThread = (IApplicationThread) contextThread;
        ....
        try 
            intent.migrateExtraStreamToClipData();
            intent.prepareToLeaveProcess(who);
           2  
            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;
    

总结上面代码 

1 拿到 IApplicationThead . aidl 进程间通信方式

2  AMS 代理对象 IActivityManager .aidl ,调用 aidl 中的 startActivity 函数最后在 AMS 中

我们来看下 ActivityManager.getService() 实现,代码如下:

//ActivityManager.java
    public static IActivityManager getService() 
        return IActivityManagerSingleton.get();
    
    private static final Singleton<IActivityManager> IActivityManagerSingleton =
            new Singleton<IActivityManager>() 
                @Override
                protected IActivityManager create() 
                  	1 
                    final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
                  	2 
                    final IActivityManager am = IActivityManager.Stub.asInterface(b);
                    return am;
                
            ;

getService 函数调用 IActivityManagerSingleton 的 get 函数 ,IActivityManagerSingleton 是一个 Singleton 类

1 得到名为“activity”的Service引用,也就是IBinder类型AMS的引用

2 注释2处将它转换成IActivityManage 的类型的对象

二 AMS 到 ApplicationThread 的调用过程 

 AMS 到 ApplicationThread 的调用流程,时序图如下:

AMS 中的 startActivity 函数,代码如下:

//AMS.java

    /**
     * Instrumentation startActivity 传递过来的
     * @param caller
     * @param callingPackage
     * @param intent
     * @param resolvedType
     * @param resultTo
     * @param resultWho
     * @param requestCode
     * @param startFlags
     * @param profilerInfo
     * @param bOptions
     * @return
     */
    @Override
    public final int startActivity(IApplicationThread caller, String callingPackage,
            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
            int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) 
        /**
         * 调用内部 startActivityAsUser 函数
         */
        return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
                resultWho, requestCode, startFlags, profilerInfo, bOptions,
                UserHandle.getCallingUserId());
    


    @Override
    public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
            int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) 
        1

        enforceNotIsolatedCaller("startActivity");
        2 
        userId = mUserController.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
                userId, false, ALLOW_FULL_ONLY, "startActivity", null);
        // TODO: Switch to user app stacks here.
        3
        
 mActivityStarter.startActivityMayWait(caller, -1, callingPackage, intent,
                resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
                profilerInfo, null, null, bOptions, false, userId, null, null,
                "startActivityAsUser");
    

在 AMS 的 startActivity 函数中调用内部的 startActivityAsUser 函数,在该函数中我们主要做了 3 个步骤

  1. 判断调用者进程是否被隔离
  2. 检查调用者权限
  3. 调用 ActivityStarter 的 startActivityMayWait 函数,启动 Activity 的理由

startActivityMayWait 函数中需要注意倒数第二个参数 TaskRecord,代表启动的 Activity 任务栈

//ActivityStarter.java
    final int startActivityMayWait(IApplicationThread caller, int callingUid,
            String callingPackage, Intent intent, String resolvedType,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            IBinder resultTo, String resultWho, int requestCode, int startFlags,
            ProfilerInfo profilerInfo, WaitResult outResult,
            Configuration globalConfig, Bundle bOptions, boolean ignoreTargetSecurity, int userId,
            IActivityContainer iContainer, TaskRecord inTask, String reason) 

...
            /**
             * ActivityStarter 是加载 Activity 控制类,会搜集所有的逻辑来决定如果将 Intent 和 Flags 转换为 Activity ,并将Activity 和 Task 以及 Stack 相关联。
             */
            int res = startActivityLocked(caller, intent, ephemeralIntent, resolvedType,
                    aInfo, rInfo, voiceSession, voiceInteractor,
                    resultTo, resultWho, requestCode, callingPid,
                    callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
                    options, ignoreTargetSecurity, componentSpecified, outRecord, container,
                    inTask, reason);


...

ActivityStarter 它是加载 Activity 的控制类,会收集所有的逻辑来决定如果将 Intent 和 Flag 转为 Activity ,并将 Activity 和 Task 以及 Stack 相关联。

看内部 startActivityLocked 方法

//ActivityStarter.java
    int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
            String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
            String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
            ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
            ActivityRecord[] outActivity, ActivityStackSupervisor.ActivityContainer container,
            TaskRecord inTask, String reason) 

        1. 判断启动的理由不为空
        if (TextUtils.isEmpty(reason)) 
            throw new IllegalArgumentException("Need to specify a reason.");
        
        mLastStartReason = reason;
        mLastStartActivityTimeMs = System.currentTimeMillis();
        mLastStartActivityRecord[0] = null;

       2. 继续调用当前类里面的 startActivity 函数
        mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,
                aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,
                callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
                options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,
                container, inTask);

        if (outActivity != null) 
            // mLastStartActivityRecord[0] is set in the call to startActivity above.
            outActivity[0] = mLastStartActivityRecord[0];
        
        return mLastStartActivityResult;
    

继续调用 startActivity 函数,代码如下:

//ActivityStarter.java

    private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
            String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
            String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
            ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
            ActivityRecord[] outActivity, ActivityStackSupervisor.ActivityContainer container,
            TaskRecord inTask) 
 ...
        1
        if (caller != null) 
            /**
             * 得到 Launcher 所在进程
             */
            callerApp = mService.getRecordForAppLocked(caller);
            /**
             *  获得 Launcher 进程的 pid , uid
             */
            if (callerApp != null) 
                callingPid = callerApp.pid;
                callingUid = callerApp.info.uid;
             else 
                Slog.w(TAG, "Unable to find app for caller " + caller
                        + " (pid=" + callingPid + ") when starting: "
                        + intent.toString());
                err = ActivityManager.START_PERMISSION_DENIED;
            
        
			...

         2
        ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,
                callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),
                resultRecord, resultWho, requestCode, componentSpecified, voiceSession != null,
                mSupervisor, container, options, sourceRecord);
        if (outActivity != null) 
           3
            outActivity[0] = r;
        

        if (r.appTimeTracker == null && sourceRecord != null) 
            // If the caller didn't specify an explicit time tracker, we want to continue
            // tracking under any it has.
            r.appTimeTracker = sourceRecord.appTimeTracker;
        

       ...

        doPendingActivityLaunchesLocked(false);

       4 
        return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true,
                options, inTask, outActivity);
    

总结 ActivityStarter startActivity 做了哪些事情

1  判断上层传递过来的 IApplicationThread 是否为空,因为这里我们是一路传递下来的,所以不为空。

2注释 1.1 处调用 AMS 的 getRecordForAppLocked 函数得到的代表 Launcher 进程的 callerApp 对象,它是 ProcessRecord 类型的, ProcessRecord 用于描述一个应用程序进程。

3 创建 ActivityRecord 类,用于描述一个 Activity 所有的信息。

4 将创建出来 ActivityRecord 赋值给 ActivityRecord 数组的第一个位置,继续调用内部重载函数 startActivity 把 启动 Activity 的描述信息也一并传递过去

接下来我们看注释 4 的代码实现

//ActivityStarter.java
    private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
            ActivityRecord[] outActivity) 
        int result = START_CANCELED;
        try 
            /**
             * 拿到 WMS
             */
            mService.mWindowManager.deferSurfaceLayout();
            1 
            result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,
                    startFlags, doResume, options, inTask, outActivity);
         finally 
            ....

        return result;
    

接着调用 ActivityStarter 内部函数 startActivityUnchecked

//ActivityStarter.java
    /**
     * 主要处理 栈管理相关的逻辑
     * @param r
     * @param sourceRecord
     * @param voiceSession
     * @param voiceInteractor
     * @param startFlags
     * @param doResume
     * @param options
     * @param inTask
     * @param outActivity
     * @return
     */
    private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
            ActivityRecord[] outActivity) 

        ...
          
       1
        if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask
                && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) 
            newTask = true;
           2 
            result = setTaskFromReuseOrCreateNewTask(
                    taskToAffiliate, preferredLaunchStackId, topStack);
         else if (mSourceRecord != null) 
            result = setTaskFromSourceRecord();
         else if (mInTask != null) 
            result = setTaskFromInTask();
         else 
            setTaskToCurrentTopOrCreateNewTask();
        
        ...
        if (mDoResume) 
            final ActivityRecord topTaskActivity =
                    mStartActivity.getTask().topRunningActivityLocked();
            if (!mTargetStack.isFocusable()
                    || (topTaskActivity != null && topTaskActivity.mTaskOverlay
                    && mStartActivity != topTaskActivity)) 
                ...
                mWindowManager.executeAppTransition();
             else 
               ...
                if (mTargetStack.isFocusable() && !mSupervisor.isFocusedStack(mTargetStack)) 
                    mTargetStack.moveToFront("startActivityUnchecked");
                
               3
                mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,
                        mOptions);
            
         else 
            mTargetStack.addRecentActivityLocked(mStartActivity);
        
       ...
        return START_SUCCESS;
    

总结上述方法 

1. 判断条件是否满足,是否是 新创建的任务栈模式

2 创建新的 TaskRecord ,用来描述 Activity 任务栈

3 调用 ActivityStackSupervisor 的 resumeFocusedStackTopActivityLocked 函数

Flag 设置为 FLAG_ACTIVITY_NEW_TASK 模式,这样注释 1 条件满足,接着执行注释 2 处的 setTaskFromReuseOrCreateNewTask 函数,其内部会创建一个新的 TaskRecord ,用来描述一个 Activity 的任务栈

现在来看3处的代码

//ActivityStackSupervisor.java
    boolean resumeFocusedStackTopActivityLocked(
            ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) 
        //需要启动的目标栈不为空,并且启动的栈跟需要启动栈一样就执行
        if (targetStack != null && isFocusedStack(targetStack)) 
            
            return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
        
         1
        final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
         2
        if (r == null || r.state != RESUMED) 
            3
            mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
         else if (r.state == RESUMED) 
            // Kick off any lingering app transitions form the MoveTaskToFront operation.
            mFocusedStack.executeAppTransition(targetOptions);
        
        return false;
    

1. 获取需要启动的 Activity 所在栈的栈顶 不是处于停止状态的 ActivityRecord

2 如果 ActivityRecord 不为 null,或者要启动的 Activity 的状态不是Resumed 状态

3 这里主要是获取正在运行的任务栈,如果获取到的栈不为空并且不是 Resumed 状态 ,那么就代表即将要启动一个新的 Activity

接着往下看 注释3处的代码

//ActivityStack.java
    boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) 
       ...
        boolean result = false;
        try 
            // Protect against recursion.
            mStackSupervisor.inResumeTopActivity = true;
            1
            result = resumeTopActivityInnerLocked(prev, options);
         finally 
            mStackSupervisor.inResumeTopActivity = false;
        
        mStackSupervisor.checkReadyForSleepLocked();

        return result;
    

把 mStackSupervisor.inResumeTopActivity 设置为 true ,然后继续调用内部 resumeTopActivityInnerLocked 函数,代码如下:

//ActivityStack.java
    private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) 
       ...

           1
            mStackSupervisor.startSpecificActivityLocked(next, true, true);
        

        if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
        return true;
    

接着看 ActivityStackSupervisor的 startSpecificActivityLocked 方法

//ActivityStackSupervisor.java
    void startSpecificActivityLocked(ActivityRecord r,
            boolean andResume, boolean checkConfig) 
        1
        ProcessRecord app = mService.getProcessRecordLocked(r.processName,
                r.info.applicationInfo.uid, true);

        r.getStack().setLaunchTime(r);

       2
        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);
                
                3
                realStartActivityLocked(r, app, andResume, checkConfig);
                return;
             catch (RemoteException e) 
               ...
            

        
				//4. 
        mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
                "activity", r.intent.getComponent(), false, false, true);
    

总结ActivityStackSupervisor的 startSpecificActivityLocked 方法

1. 获取即将启动的 Activity 所在的应用程序进程

2.判断要启动的 Activity 所在的应用程序进程

3.如果已经正在运行调用 realStartActivityLocked 函数

4会通知 AMS 与 Zygote 通信请求创建一个新的进程

//ActivityStackSupervisor.java
    final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
            boolean andResume, boolean checkConfig) throws RemoteException 
			
      ...
            
        /*** 
        			这里的 app.thread 代表的是 IApplicationThread ,调用它内部 scheduleLaunchActivity 函								数,
             * 它的实现在 ActivityThread 的内部类 ApplicationThread ,其中 ApplicationThread 继承了 IApplicationThread.Stub.,
             * app 指的是Activity 所在的应用程序进程。
             */

            app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
                    System.identityHashCode(r), r.info,
                    // TODO: Have this take the merged configuration instead of separate global and
                    // override configs.
                    mergedConfiguration.getGlobalConfiguration(),
                    mergedConfiguration.getOverrideConfiguration(), r.compat,
                    r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
                    r.persistentState, results, newIntents, !andResume,
                    mService.isNextTransitionForward(), profilerInfo);

            ...

         catch (RemoteException e) 
            ...

        return true;
    

app.thread 指的就是 IApplicationThread, 它的实现类在 ActivityThread 的内部类,其中 ApplicationThread 继承了 IApplicationThread.Stub。app 指的是传入的要启动的 Activity 所在应用程序进程,因此,这段代码指的就是要在目标应用进程进行启动 Activity 。当前代码逻辑运行在 AMS 所在的 SystemServer 进程中,通过 ApplicationThread 来与应用程序进程进行 Binder 通信,换句话说,ApplicationThread 是 SystemServer 和应用程序进程通信的中间人。
到这里下面就该看 ActivityThread 类里面执行 Activity 任务了,

三  ActivityThread 启动 Activity 过程

先来看一下 ActivityThread 启动 Activity 的时序图

ActivityThread 在应用程序进程创建成功后会运行在当前进程的主线程中。scheduleLaunchActivity 代码如下:

//ActivityThread.java
public final class ActivityThread 
 ...
   
 private class ApplicationThread extends IApplicationThread.Stub   
   
 ...
 
 private class ApplicationThread extends IApplicationThread.Stub 
   
 ....
   
         /**
         * Launcher 点击应用图标启动对应的应用 .... -> ActivityStackSupervisor . realStartActivityLocked 函数调用
         * @param intent
         * @param token
         * @param ident
         * @param info
         * @param curConfig
         * @param overrideConfig
         * @param compatInfo
         * @param referrer
         * @param voiceInteractor
         * @param procState
         * @param state
         * @param persistentState
         * @param pendingResults
         * @param pendingNewIntents
         * @param notResumed
         * @param isForward
         * @param profilerInfo
         */
        @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);
           1. 调用内部 sendMessage 函数,将启动 Activity 的信息封装成 ActivityClientRecord 对象,然后跟 H.LAUNCH_ACTIVITY 一起传递下去
            sendMessage(H.LAUNCH_ACTIVITY, r);
        
   
 ...
 
   
 ...

  //ActivityThread.java  
	private void sendMessage(int what, Object obj) 
         调用内部重载函数 what = H.LAUNCH_ACTIVITY
        sendMessage(what, obj, 0, 0, false);
    
//ActivityThread.java

    private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) 
        if (DEBUG_MESSAGES) Slog.v(
            TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
            + ": " + arg1 + " / " + obj);
        Message msg = Message.obtain();
        msg.what = what;
        msg.obj = obj;
        msg.arg1 = arg1;
        msg.arg2 = arg2;
        if (async) 
            msg.setAsynchronous(true);
        
       2. 通过 H 的 Handler 将 what = H.LAUNCH_ACTIVITY 消息 what 发送出去
        mH.sendMessage(msg);
    

mH 是 AcitivtyThread 非静态内部类,它继承自 Handler,来看下 H 的实现

//ActivityThread.java
public final class ActivityThread 
  
//应用程序进程 主线程 H 
final H mH = new H(); 
  
  ......
    
 private class H extends Handler 
 
  //启动 Activity 标志
  public static final int LAUNCH_ACTIVITY         = 100;
    
    public void handleMessage(Message msg) 
            if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
            switch (msg.what) 
                1 
                case LAUNCH_ACTIVITY: 
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
                    2
                    final ActivityClientRecord r = (ActivityClientRecord) msg.obj;

                   3
                    r.packageInfo = getPackageInfoNoCheck(
                            r.activityInfo.applicationInfo, r.compatInfo);
                    4 
                    handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                 break;
                
                
               ....
  ......

我们看下H 主要干了什么

1. 收到发送过来 LAUNCH_ACTIVITY 标志的消息

2 拿到封装启动 Activity 信息的 ActivityClientRecord 类

3 通过 getPackageInfoNoCheck 函数获得 LoadedApk 类型的对象并赋值给 ActivityClientRecord 的成员变量 packageInfo。 * 应用程序进程要启动 Activity 时,需要将该 Activity 所属的 APK 加载进来,而 LoadedApk 就是用来描述已加载的 APk 文件的

4 调用内部 handleLaunchActivity

接着往下看 注释4 处的代码 handleLaunchActivity

//ActivityThread.java
    /**
     *
     *启动 Activity
     * @param r
     * @param customIntent
     * @param reason
     */
    private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) 
       ...

        1. 实施启动 Activity 的实例
        Activity a = performLaunchActivity(r, customIntent);

        if (a != null) 
            r.createdConfig = new Configuration(mConfiguration);
            reportSizeConfigurations(r);
            Bundle oldState = r.state;
          2. 将 Activity 的状态设置为 Resume
            handleResumeActivity(r.token, false, r.isForward,
                    !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);

         ...
         else 
           
            try 
               
                ActivityManager.getService()
                    .finishActivity(r.token, Activity.RESULT_CANCELED, null,
                            Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
             catch (RemoteException ex) 
                throw ex.rethrowFromSystemServer();
            
        
    

接着往下看  performLaunchActivity 

//ActivityThread.java


    private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) 
        // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");

      1. 通过封装启动 Activity 信息类 ActivityClientRecord 来获取到 ActivityInfo
        ActivityInfo aInfo = r.activityInfo;
       2. 如果 r.packageInfo == null 就获取 LoadedApk 实例
        if (r.packageInfo == null) 
            r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
                    Context.CONTEXT_INCLUDE_CODE);
        

        3. 通过封装启动 Activity 信息类 ActivityClientRecord 来获取启动 Activity 组件的 ComponentName
        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);
        
         4. 创建Activity 中的上下文环境
        ContextImpl appContext = createBaseContextForActivity(r);
        Activity activity = null;
        try 
           5. 调用 Instrumentation.newActivity 函数 ,内部通过 ClassLoader 类加载器来创建 Activity 的实例
            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 
            6. 得到 Application 对象
            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) 

                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;
                
                appContext.setOuterContext(activity);
               7. 将 appContext 等对象依附在 Activity 的 attach 函数中,进行 Activity  attach 初始化
                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, r.configCallback);

                if (customIntent != null) 
                    activity.mIntent = customIntent;
                
                r.lastNonConfigurationInstances = null;
                checkAndBlockForNetworkAccess();
                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 
                 8. 内部调用 Activity 的 onCreate 方法
                    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;
    

拿到启动 Activity 组件 Component 的信息,通过 Instrumentation 类的 newActivity 函数,内部是通过 ClassLoader 的 loadClass 实例化了 Activity, 拿到 Application 进行与 Activity 绑定,最后调用 Instrumentation 函数的 callActivityOnCreate 执行了 Activity onCreate 生命周期,到这里 Launcher 点击应用图标到启动应用程序的根 Activity 已经启动了 至此Activity的启动分析完了。

以上是关于Android 源码分析 Activity 启动的主要内容,如果未能解决你的问题,请参考以下文章

Android 启动过程Activity 启动源码分析 ( AMS -> ActivityThreadAMS 线程阶段 )

基于android10.0源码分析Activity的启动流程

android6.0源码分析之Activity启动过程

android6.0源码分析之Activity启动过程

Android 启动过程Activity 启动源码分析 ( AMS -> ActivityThreadAMS 线程阶段 二 )

Android 源码分析 Activity 启动