Context 表示上下文痹愚。
Android中的 Context 使用了裝飾模式籽御。
Android 中 Context 的實(shí)現(xiàn)類目前只有唯一的 ContextImpl ,其構(gòu)造方法是 private 的话浇,但是對(duì)外提供了3個(gè) static 用以 createContextImpl
//如果該進(jìn)程是 system 進(jìn)程增蹭,則會(huì)調(diào)用此方法生成 Context
static ContextImpl createSystemContext(ActivityThread mainThread) {
LoadedApk packageInfo = new LoadedApk(mainThread);
ContextImpl context = new ContextImpl(null, mainThread,
packageInfo, null, null, false, null, null, Display.INVALID_DISPLAY);
context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
context.mResourcesManager.getDisplayMetricsLocked());
return context;
}
//Application 或者 Service 調(diào)用
static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
return new ContextImpl(null, mainThread,
packageInfo, null, null, false, null, null, Display.INVALID_DISPLAY);
}
//Activity調(diào)用
static ContextImpl createActivityContext(ActivityThread mainThread,
LoadedApk packageInfo, int displayId, Configuration overrideConfiguration) {
if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
return new ContextImpl(null, mainThread, packageInfo, null, null, false,
null, overrideConfiguration, displayId);
}
private ContextImpl(ContextImpl container, ActivityThread mainThread, LoadedApk packageInfo, IBinder activityToken, UserHandle user, boolean restricted, Display display,
Configuration overrideConfiguration, int createDisplayWithId){
...
}
//對(duì)于 Application 中的 Context
//makeApplication()@LoadedApk
public Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation){
...
// 這個(gè) Context 就是傳到 Application 中的
ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
app = mActivityThread.mInstrumentation.newApplication(cl, appClass, appContext);
...
}
static ContextImple createAppContext(ActivityThread mainThread, LoadedApk packageInfo){
return new Context(null, mainThread,packageInfo, null, null, false, null,null, Display.INVALID_DISPLAY)杆逗;
}
所以在 Application 的 Context 中
- ContextImpl contatiner = null;
- ActivityThread mainThread = mainThread;
- LoadedPack packageInfo = packageInfo;
- IBinder activityToken = null;
- UserHandle user = null;
- boolean restricted = flase;
- Display display = null;
- Configuration overrideConfiguration = null;
- int creatDisplayWithId = Display.INVALID_DISPLAY;
activity中的 context
//performActivity@ActivityThread
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent){
...
Context appContext = createBaseContextForActivity(r, activity);
...
}
private Context createBaseContextForActivity(ActivityRecord r, final Activity activity){
ContextImpl appContext = ContextImple.createActivityContext(this, r.packageInfo, displayId, r.overrideConfig);
}
static ContextImpl createActivityContext(ActivityThread mainThread, LoadedApk packageInfo, int displayId, Configuration overrideConfiguration){
return new ContextImple(null, mainThred, packageInfo, null, null, false, null, overrideConfiguration, displayId);
}
- ContextImple container = null;
- ActivityThread mainThread = mainThread;
- LoadedApk packageInfo = packageInfo;
- IBinder activityToken = null;
- UserHandle user = null;
- boolean restricted = false;
- Display display = null;
- OverrideConfiguration overrideConfiguration = overrideConfiguration;
- int createDisplayWithId = displayId;
Service的context
//handleCreateService@ActivityThread
private void handleCreateService(CreateServiceData data){
ContextImpl context = ContextImpl.createAppContext(this,packageInfo);
}