文章參考《Android進(jìn)階解密》一書
一、概述
Context也就是上下文對(duì)象,是Android的常用類,Android四大組件都會(huì)涉及到Context揍鸟,比如我們啟動(dòng)Service會(huì)調(diào)用ContextWrapper以及ContextImpl的startService方法,ContextWrapper以及ContextImpl就是Context的關(guān)聯(lián)類燥爷。
二蜈亩、Context的關(guān)聯(lián)類
Context意為上下文懦窘,是一個(gè)應(yīng)用程序環(huán)境信息的接口前翎。在我們使用時(shí)一般分為兩種:
- 使用Context調(diào)用方法,比如啟動(dòng)Activity畅涂、訪問資源港华、調(diào)用系統(tǒng)級(jí)服務(wù)等
- 調(diào)用方法時(shí)傳入Context,比如彈出Toast午衰、Dialog等
Activity立宜、Service、Application都間接地繼承自Context臊岸,因此我們可以計(jì)算出一個(gè)應(yīng)用程序的Context數(shù)量橙数,等于Activity和Service的數(shù)量加1,1指的是Application數(shù)量。
Context是一個(gè)抽象類帅戒,它的內(nèi)部定義了很多方法以及靜態(tài)常量灯帮,它的具體實(shí)現(xiàn)類為ContextImpl。和Context相關(guān)聯(lián)的類逻住,除了ContextImpl,還有ContextWrapper钟哥、ContextThemeWrapper和Activity等,如下圖:
從上圖可以看出ContextWrapper瞎访、Context繼承自Context腻贰,ContextWrapper內(nèi)部包含Context類型的mBase對(duì)象,mBase對(duì)象只想ContextImpl.ContextImpl提供了很多功能扒秸,ContextWrapper是一個(gè)裝飾類播演,對(duì)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)不會(huì)暴露給使用者古徒,使用者也無需要關(guān)心ContextImpl的實(shí)現(xiàn)拓提。
- 通過組合而非繼承的方式,拓展ContextImpl的功能隧膘,在運(yùn)行時(shí)選擇不同的裝飾類代态,實(shí)現(xiàn)不同的功能。
下面我們來分別講解一下個(gè)Context的創(chuàng)建過程疹吃。
三蹦疑、Application Context的創(chuàng)建過程
調(diào)用getApplicationContext來獲取全局的Application Context。應(yīng)用程序啟動(dòng)完成后萨驶,應(yīng)用程序就會(huì)有一個(gè)全局的Application Context歉摧。我們從應(yīng)用程序的啟動(dòng)過程開始。<br />ActivityThread作為應(yīng)用程序的主線程管理類腔呜,它會(huì)調(diào)用它的內(nèi)部類ApplicationThread色scheduleLaunchActivity方法來啟動(dòng)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)用啟動(dòng)以后啟動(dòng)Activity會(huì)通過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;
}
這里簡(jiǎn)化了一下代碼,最后會(huì)調(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時(shí),直接返回mApplication新娜,如果第一次啟動(dòng)程序mApplication為null赵辕,通過ContextImpl.createAppContext來創(chuàng)建ContextImpl。mActivityThread.mInstrumentation.newApplication中傳入?yún)?shù)ClassLoader和上面的ContextImpl對(duì)象創(chuàng)建Application概龄,然后又將app賦值給ContextImpl的mOuterContext还惠,這樣COntext也持有Application的引用了,最后將Application賦值給LoadedApk的mApplication私杜,mApplication就是Application類型的對(duì)象蚕键。下面的代碼就是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對(duì)象傳進(jìn)去充活。這后面跟Application差不多,會(huì)在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í)跟上面兩個(gè)差不多,只不過是在Service類里面執(zhí)行宿接,此處就不過多敘述了赘淮。大家自己去看源碼就可以了