Context

Context 表示上下文。
Android中的 Context 使用了装饰模式。

image.png

Android 中 Context 的实现类目前只有唯一的 ContextImpl ,其构造方法是 private 的,但是对外提供了3个 static 用以 createContextImpl

 //如果该进程是 system 进程,则会调用此方法生成 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 调用
    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调用
    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){
  ...
}

//对于 Application 中的 Context

//makeApplication()@LoadedApk
public Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation){
  ...
  //  这个 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);
}

你可能感兴趣的:(Context)