美文网首页
activity启动流程(8.0源码)

activity启动流程(8.0源码)

作者: 追寻米K | 来源:发表于2019-01-10 16:43 被阅读0次

    不管是相同应用还是不同应用启动两个activity(可以把系统也看成是一个应用),都是调用startActivity开始启动。

                 //该应用的包名
                String pkg = info.activityInfo.packageName;
                //应用的主activity类
                String cls = info.activityInfo.name;
    
                ComponentName componet = new ComponentName(pkg, cls);
    
                Intent i = new Intent();
                i.setComponent(componet);
                startActivity(i);
    

    这是启动另外一个APP的方法。

    流程图: activity启动流程.jpg

    Instrumentation类

    execStartActivity

    public ActivityResult execStartActivity(
                Context who, IBinder contextThread, IBinder token, Activity target,
                Intent intent, int requestCode, Bundle options) {
        ......
     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;
        }
    

    ActivityManager只是一个单独的类

    public class ActivityManager {......}
    

    ActivityManager的getService方法

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

    IActivityManager 就是一个aidl文件对象,而ActivityManagerService也是继承了IActivityManager.Stub(API 26已经变了不再是继承ActivityManagerNative)

    public class ActivityManagerService extends IActivityManager.Stub
            implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {......}
    

    通过AIDL返回ActivityManagerService的IActivityManager这个代理 对象,通过这个代理调用ActivityManagerService中的方法。

    ActivityManagerService类

    public int startActivity(IBinder whoThread, String callingPackage,
                    Intent intent, String resolvedType, Bundle bOptions) {
    ..........
    return mActivityStarter.startActivityMayWait(appThread, -1, callingPackage, intent,
                        resolvedType, null, null, null, null, 0, 0, null, null,
                        null, bOptions, false, callingUser, tr, "AppTaskImpl");
    }
    

    ActivityStarter类

    startActivityMayWait 获得启动activity的一些系统参数如uid,进程的pid等等,跟传入的Intent重新组合一个newIntent传入启动流程中。

    final ActivityRecord[] outRecord = new ActivityRecord[1]
    

    startActivity(294行)

    初始化ActivityRecord 对象

      ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,
                    callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),
                    resultRecord, resultWho, requestCode, componentSpecified, voiceSession != null,
                    mSupervisor, options, sourceRecord);
    

    startActivityUnchecked

    startActivityUnchecked方法太复杂了,涉及activity的启动模式Intent的flag等诸多要素,如其中一个方法computeLaunchingTaskFlags()

    if (mLaunchSingleInstance || mLaunchSingleTask) {......}
    

    是不是有点眼熟,SingleInstance、SingleTask都是activity的启动模式之一。
    最后会调用

    if (mDoResume) {
    ........
    }else {
     if (mTargetStack.isFocusable() && !mSupervisor.isFocusedStack(mTargetStack)) {
             mTargetStack.moveToFront("startActivityUnchecked");
     }
             mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,
                            mOptions);
    }
    

    resumeFocusedStackTopActivityLocked

    //如果待启动activity的ActivityStack是当前的前台Stack
     if (targetStack != null && isFocusedStack(targetStack)) {
                return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
            }
    
            final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
    //否则调用当前前台ActivityStack的resumeTopActivityUncheckedLocked
            if (r == null || r.state != RESUMED) {
                mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
            } else if (r.state == RESUMED) {
                // Kick off any lingering app transitions form the MoveTaskToFront operation.
                mFocusedStack.executeAppTransition(targetOptions);
            }
    

    ActivityStack类

    boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
    ......
    boolean result = false;
            try {
                // Protect against recursion.
                mStackSupervisor.inResumeTopActivity = true;
                result = resumeTopActivityInnerLocked(prev, options);
            } finally {
                mStackSupervisor.inResumeTopActivity = false;
            }
    ......
        return result;
        }
    

    resumeTopActivityInnerLocked

    这个方法非常复杂

    private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
    ......
    // Find the next top-most activity to resume in this stack that is not finishing and is
            // focusable. If it is not focusable, we will fall into the case below to resume the
            // top activity in the next focusable task.
            final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
    ......
    if (next.app != null && next.app.thread != null) {
    ......
    //如果activity启动过了就ResumeActivity
      next.app.pendingUiClean = true;
                        next.app.forceProcessStateUpTo(mService.mTopProcessState);
                        next.clearOptionsLocked();
                        next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
                                mService.isNextTransitionForward(), resumeAnimOptions);
    ......
    }else{
    ......
    mStackSupervisor.startSpecificActivityLocked(next, true, true);
    }
    

    ActivityStackSupervisor类

    startSpecificActivityLocked

     void startSpecificActivityLocked(ActivityRecord r,
                boolean andResume, boolean checkConfig) {
            // Is this activity's application already running?
            ProcessRecord app = mService.getProcessRecordLocked(r.processName,
                    r.info.applicationInfo.uid, true);
    
            r.getStack().setLaunchTime(r);
    //如果进程已经存在,也就是APP启动过
    if (app != null && app.thread != null) {
                try {
                    if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
                            || !"android".equals(r.info.packageName)) {
                        // Don't add this if it is a platform component that is marked
                        // to run in multiple processes, because this is actually
                        // part of the framework so doesn't make sense to track as a
                        // separate apk in the process.
                        app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
                                mService.mProcessStats);
                    }
                    realStartActivityLocked(r, app, andResume, checkConfig);
                    return;
                } catch (RemoteException e) {
                    Slog.w(TAG, "Exception when starting activity "
                            + r.intent.getComponent().flattenToShortString(), e);
                }
    
                // If a dead object exception was thrown -- fall through to
                // restart the application.
            }
    //否则使用zygote创建一个进程
            mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
                    "activity", r.intent.getComponent(), false, false, true);
        }
    

    realStartActivityLocked

       final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
                boolean andResume, boolean checkConfig) throws RemoteException {
    ......
    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);
    ......
    }
    

    app.thread是IApplicationThread对象,而ActivityThread的内部类ApplicationThread继承了IApplicationThread.Stub,app是当前ProcessRecord对象,也就是进程,每个进程都绑定一个ApplicationThread

    private class ApplicationThread extends IApplicationThread.Stub {
    ......
    RuntimeInit.setApplicationObject(mAppThread.asBinder());
    ......
    }
    

    所以通过Binder机制scheduleLaunchActivity调用的其实是ApplicationThread 的方法。

    ActivityThread

    内部类ApplicationThread的scheduleLaunchActivity

    使用Handler机制发送一个LAUNCH_ACTIVITY的消息

    public final void scheduleLaunchActivity(......){
    .......
    sendMessage(H.LAUNCH_ACTIVITY, r);
    }
    
    public void handleMessage(Message msg) {
    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;
    ......
    }
    

    handleLaunchActivity

    private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
    ......
    Activity a = performLaunchActivity(r, customIntent);
    if (a != null) {
     handleResumeActivity(r.token, false, r.isForward,
                        !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
    ......
      }else{
    ......
      }
    }
    

    1、先来看一下performLaunchActivity

     private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    ......
    //创建当前Activity的Context的实现类ContextImpl
    ContextImpl appContext = createBaseContextForActivity(r);
    Activity activity = null;
            try {
                java.lang.ClassLoader cl = appContext.getClassLoader();
    //类加载器初始化一个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) {
    ......
        }
     try {
        Application app = r.packageInfo.makeApplication(false,   mInstrumentation);
    if (activity != 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, r.configCallback);
    ......
     if (r.isPersistable()) {
         mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
        } else {
          mInstrumentation.callActivityOnCreate(activity, r.state);
       }
    
      }catch (Exception e) {
    ......
      }
    }
    
     public Activity newActivity(ClassLoader cl, String className,
                Intent intent)
                throws InstantiationException, IllegalAccessException,
                ClassNotFoundException {
            return (Activity)cl.loadClass(className).newInstance();
        }
    

    makeApplication方法创建应用的Application对象,并调用Application的onCreate方法

    public Application makeApplication(boolean forceDefaultAppClass,
                Instrumentation instrumentation) {
    ......
    //如果自定义了Application就是自定义的,否则默认一个
     String appClass = mApplicationInfo.className;
            if (forceDefaultAppClass || (appClass == null)) {
                appClass = "android.app.Application";
            }
    java.lang.ClassLoader cl = getClassLoader();
    ......
    //创建Application并调用Application的attach方法
    app = mActivityThread.mInstrumentation.newApplication(
                        cl, appClass, appContext);
    ......
    //调用Application的onCreate
    instrumentation.callApplicationOnCreate(app);
    }
    

    callActivityOnCreate方法调用activity的performCreate

       public void callActivityOnCreate(Activity activity, Bundle icicle,
                PersistableBundle persistentState) {
            prePerformCreate(activity);
            activity.performCreate(icicle, persistentState);
            postPerformCreate(activity);
        }
    
    final void performCreate(Bundle icicle, PersistableBundle persistentState) {
    ......
    if (persistentState != null) {
                onCreate(icicle, persistentState);
            } else {
                onCreate(icicle);
            }
    ......
    }
    
     final void performCreate(Bundle icicle, PersistableBundle persistentState) {
    ......
    if (persistentState != null) {
                onCreate(icicle, persistentState);
            } else {
                onCreate(icicle);
            }
    ......
    }
    

    2、handleResumeActivity

     final void handleResumeActivity(IBinder token,
                boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
    ......
    if (r.window == null && !a.mFinished && willBeVisible) {
                    r.window = r.activity.getWindow();
                    View decor = r.window.getDecorView();
                    decor.setVisibility(View.INVISIBLE);
                    ViewManager wm = a.getWindowManager();
                    WindowManager.LayoutParams l = r.window.getAttributes();
                    a.mDecor = decor;
                    l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
                    l.softInputMode |= forwardBit;
                    if (r.mPreserveWindow) {
                        a.mWindowAdded = true;
                        r.mPreserveWindow = false;
                        ViewRootImpl impl = decor.getViewRootImpl();
                    } 
    ......
      }
    ......
    }
    

    这个是不是很熟悉,在View的绘制流程中的window、DecorView、ViewRootImpl 。

    如果app从未被启动

    主入口ActivityThread的调用attach方法

    public static void main(String[] args) {
    ......
            ActivityThread thread = new ActivityThread();
            thread.attach(false);
    ......
    }
    
    private void attach(boolean system) {
    RuntimeInit.setApplicationObject(mAppThread.asBinder());
                final IActivityManager mgr = ActivityManager.getService();
                try {
                    mgr.attachApplication(mAppThread);
                } catch (RemoteException ex) {
                    throw ex.rethrowFromSystemServer();
                }
    ......
    }
    

    又是熟悉的Binder机制,mgr实际是ActivityManagerService的代理对象,并把ApplicationThread传入ActivityManagerService,这样ActivityManagerService就通过ApplicationThread这个桥梁和ActivityThread关联起来了。

     public final void attachApplication(IApplicationThread thread) {
            synchronized (this) {
                //获得当前进程的id
                int callingPid = Binder.getCallingPid();
                final long origId = Binder.clearCallingIdentity();
                attachApplicationLocked(thread, callingPid);
                Binder.restoreCallingIdentity(origId);
            }
        }
    
    private final boolean attachApplicationLocked(IApplicationThread thread,
                int pid) {
    .......
    //绑定Application
      if (app.instr != null) {
                    thread.bindApplication(processName, appInfo, providers,
                            app.instr.mClass,
                            profilerInfo, app.instr.mArguments,
                            app.instr.mWatcher,
                            app.instr.mUiAutomationConnection, testMode,
                            mBinderTransactionTrackingEnabled, enableTrackAllocation,
                            isRestrictedBackupMode || !normalMode, app.persistent,
                            new Configuration(getGlobalConfiguration()), app.compat,
                            getCommonServicesLocked(app.isolated),
                            mCoreSettingsObserver.getCoreSettingsLocked(),
                            buildSerial);
                } else {
                    thread.bindApplication(processName, appInfo, providers, null, profilerInfo,
                            null, null, null, testMode,
                            mBinderTransactionTrackingEnabled, enableTrackAllocation,
                            isRestrictedBackupMode || !normalMode, app.persistent,
                            new Configuration(getGlobalConfiguration()), app.compat,
                            getCommonServicesLocked(app.isolated),
                            mCoreSettingsObserver.getCoreSettingsLocked(),
                            buildSerial);
                }
    ......
     // See if the top visible activity is waiting to run in this process...
            if (normalMode) {
                try {
                    //启动Activity
                    if (mStackSupervisor.attachApplicationLocked(app)) {
                        didSomething = true;
                    }
                } catch (Exception e) {
                    Slog.wtf(TAG, "Exception thrown launching activities in " + app, e);
                    badApp = true;
                }
            }
    ......
    }
    

    bindApplication

    thread是IApplicationThread代理对象,ActivityThread通过ApplicationThread和ActivityManagerService通信,所以bindApplication是ApplicationThread的方法

    private class ApplicationThread extends IApplicationThread.Stub {
    public final void bindApplication(...参数...){
        ......
         sendMessage(H.BIND_APPLICATION, data);
      }
    }
    
    public void handleMessage(Message msg) {
      .....
      case BIND_APPLICATION:
                        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
                        AppBindData data = (AppBindData)msg.obj;
                        handleBindApplication(data);
                        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                        break;
      ......
    }
    
    private void handleBindApplication(AppBindData data) {
      ......
     //创建进程对应的Android运行环境ContextImpl
      final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
    ......
    if (ii != null) {
    ......
    //初始化LoadedApk 对象
    final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
                        appContext.getClassLoader(), false, true, false);
    final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
    try {
    //初始化Instrumentation
                    final ClassLoader cl = instrContext.getClassLoader();
                    mInstrumentation = (Instrumentation)
                        cl.loadClass(data.instrumentationName.getClassName()).newInstance();
                } catch (Exception e) {
          ......
        }
      }else {
            //或者在这初始化Instrumentation
                mInstrumentation = new Instrumentation();
            }
    ......
    //初始化Application
    app = data.info.makeApplication(data.restrictedBackupMode, null);
    ......
    //调用Application的onCreate方法
    mInstrumentation.callApplicationOnCreate(app);
    ......
    }
    

    attachApplicationLocked

    ActivityStackSupervisor类中的方法attachApplicationLocked

    boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
      ......
      if (realStartActivityLocked(activity, app,
                                        top == activity /* andResume */, true /* checkConfig */)) {
                                    didSomething = true;
                                }
      ......
    }
    

    又是realStartActivityLocked又是一个轮回,调用真的启动Activity的方法。
    参考博客链接:https://blog.csdn.net/qian520ao/article/details/78156214

    相关文章

      网友评论

          本文标题:activity启动流程(8.0源码)

          本文链接:https://www.haomeiwen.com/subject/jthiqqtx.html