Context創(chuàng)建過程解析

文章參考《Android進(jìn)階解密》一書

一、概述

Context也就是上下文對象,是Android的常用類,Android四大組件都會涉及到Context,比如我們啟動Service會調(diào)用ContextWrapper以及ContextImpl的startService方法,ContextWrapper以及ContextImpl就是Context的關(guān)聯(lián)類。

二、Context的關(guān)聯(lián)類

Context意為上下文,是一個應(yīng)用程序環(huán)境信息的接口。在我們使用時一般分為兩種:

  • 使用Context調(diào)用方法,比如啟動Activity、訪問資源、調(diào)用系統(tǒng)級服務(wù)等
  • 調(diào)用方法時傳入Context,比如彈出Toast、Dialog等

Activity、Service、Application都間接地繼承自Context,因此我們可以計算出一個應(yīng)用程序的Context數(shù)量,等于Activity和Service的數(shù)量加1,1指的是Application數(shù)量。

Context是一個抽象類,它的內(nèi)部定義了很多方法以及靜態(tài)常量,它的具體實(shí)現(xiàn)類為ContextImpl。和Context相關(guān)聯(lián)的類,除了ContextImpl,還有ContextWrapper、ContextThemeWrapper和Activity等,如下圖:


image.png

從上圖可以看出ContextWrapper、Context繼承自Context,ContextWrapper內(nèi)部包含Context類型的mBase對象,mBase對象只想ContextImpl.ContextImpl提供了很多功能,ContextWrapper是一個裝飾類,對ContextImpl進(jìn)行包裝,ContextWrapper主要是起到了方法傳遞的功能,ContextWrapper幾乎所有的方法都是調(diào)用ContextImpl的相應(yīng)方法實(shí)現(xiàn)的。ContextThemeWrapper、Activity、Service都繼承自ContextWrapper,這樣它們就可以通過mBase使用Context的功能了。Activity有主題的相關(guān)設(shè)置,所以繼承了ContextThemeWrapper里面有設(shè)置或者gettheme的方法。<br />Context的關(guān)聯(lián)類采用了裝飾模式,主要有以下優(yōu)點(diǎn):

  • 使用者可以更方便的使用Context
  • 如果ContextImpl發(fā)生改變,不需要修改其它地方的代碼
  • ContextImpl的實(shí)現(xiàn)不會暴露給使用者,使用者也無需要關(guān)心ContextImpl的實(shí)現(xiàn)。
  • 通過組合而非繼承的方式,拓展ContextImpl的功能,在運(yùn)行時選擇不同的裝飾類,實(shí)現(xiàn)不同的功能。

下面我們來分別講解一下個Context的創(chuàng)建過程。

三、Application Context的創(chuàng)建過程

調(diào)用getApplicationContext來獲取全局的Application Context。應(yīng)用程序啟動完成后,應(yīng)用程序就會有一個全局的Application Context。我們從應(yīng)用程序的啟動過程開始。<br />ActivityThread作為應(yīng)用程序的主線程管理類,它會調(diào)用它的內(nèi)部類ApplicationThread色scheduleLaunchActivity方法來啟動Activity

 // we use token to identify this activity without having to send the
        // activity itself back to the activity manager. (matters more with ipc)
        @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);
        }

應(yīng)用啟動以后啟動Activity會通過Binder跨進(jìn)程調(diào)用ActivityManagerService(AMS)里面,AMS里面持有ApplicationThread的IBinder引用,跨進(jìn)程調(diào)用scheduleLaunchActivity方法,scheduleLaunchActivity最后通過Handler發(fā)送H.LAUNCH_ACTIVITY消息,最后調(diào)用ActivityThread的handleLaunchActivity方法。

  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);
            ............
            }
        } else {
            // If there was an error, for any reason, tell the activity manager to stop us.
            try {
                ActivityManager.getService()
                    .finishActivity(r.token, Activity.RESULT_CANCELED, null,
                            Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }
        }
    }

然后調(diào)用performLaunchActivity方法

 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
        // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
        。。。。。。。
        try {
            Application app = r.packageInfo.makeApplication(false, mInstrumentation);
        。。。。。。。
        return activity;
    }

這里簡化了一下代碼,最后會調(diào)用ActivityClientRecord.packageInfo.makeApplication()

  public Application makeApplication(boolean forceDefaultAppClass,
            Instrumentation instrumentation) {
        if (mApplication != null) {
            return mApplication;
        }

        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "makeApplication");

        Application app = null;

        String appClass = mApplicationInfo.className;
        if (forceDefaultAppClass || (appClass == null)) {
            appClass = "android.app.Application";
        }

        try {
            。。。。。。。
            ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
            app = mActivityThread.mInstrumentation.newApplication(
                    cl, appClass, appContext);
            appContext.setOuterContext(app);
        } catch (Exception e) {
            。。。。。。。。
        }
        mActivityThread.mAllApplications.add(app);
        mApplication = app;

        if (instrumentation != null) {
            try {
                instrumentation.callApplicationOnCreate(app);
            } catch (Exception e) {
            。。。。。。。。。。。    
            }
        }
        return app;
    }

當(dāng)mApplication不為null時,直接返回mApplication,如果第一次啟動程序mApplication為null,通過ContextImpl.createAppContext來創(chuàng)建ContextImpl。mActivityThread.mInstrumentation.newApplication中傳入?yún)?shù)ClassLoader和上面的ContextImpl對象創(chuàng)建Application,然后又將app賦值給ContextImpl的mOuterContext,這樣COntext也持有Application的引用了,最后將Application賦值給LoadedApk的mApplication,mApplication就是Application類型的對象。下面的代碼就是Application的創(chuàng)建方法:

  static public Application newApplication(Class<?> clazz, Context context)
            throws InstantiationException, IllegalAccessException, 
            ClassNotFoundException {
        Application app = (Application)clazz.newInstance();
        app.attach(context);
        return app;
    }

這里是通過反射來創(chuàng)建Application,并調(diào)用了attach方法,將ContextImpl傳進(jìn)去,最后返回該Application。

 /**
     * @hide
     */
    /* package */ final void attach(Context context) {
        attachBaseContext(context);
        mLoadedApk = ContextImpl.getImpl(context).mPackageInfo;
    }

這里調(diào)用了attachBaseContext(context),在Application的父類ContextWrapper里面調(diào)用

  protected void attachBaseContext(Context base) {
        if (mBase != null) {
            throw new IllegalStateException("Base context already set");
        }
        mBase = base;
    }

將ContextImpl賦值給mBase,ContextImpl也是Context的實(shí)現(xiàn)類,將ContextImpl賦值給ContextWrapper的mBase,這樣ContextWrapper就可以用mBase調(diào)用Context里面的方法了,而Application繼承自ContextWrapper,所以Application也可以調(diào)用Context里面的方法了。

四、Activity Context的創(chuàng)建過程

Activity的Context也是在performLaunchActivity方法創(chuàng)建的。

 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
        // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
     
       。。。。。。
        ContextImpl appContext = createBaseContextForActivity(r);
        Activity activity = null;
        try {
            java.lang.ClassLoader cl = appContext.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) {
                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);
                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);
            }
        }       
        return activity;
    }

首先通過 createBaseContextForActivity(r)創(chuàng)建ContextImpl實(shí)例appContext,將創(chuàng)建的Activity設(shè)置到ContextImpl的outContext,這樣Context里面就可以訪問Activity的變量和方法,最后Activity的attach方法,將ContextImpl對象傳進(jìn)去。這后面跟Application差不多,會在Activity的attach方法調(diào)用attachBaseContext(context)方法,將ContextImpl復(fù)制給ContextWrapper的mBase引用,Activity繼承自ContextWrapper,這樣Activity就可以調(diào)用Context里面的方法了。

五、Service Context的創(chuàng)建過程

Service的Context創(chuàng)建其實(shí)跟上面兩個差不多,只不過是在Service類里面執(zhí)行,此處就不過多敘述了。大家自己去看源碼就可以了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容