从应用场景的角度来讲,他是一个场景,一个用户与系统交互的过程.好比当你看短信时,场景包括短信的页面,以及隐藏在后面的数据java
提到页面,咱们应该可以联想到 Activityapp
没错,Activity,Service都是一个Contextide
从JAVA语言角度来讲,Context是一个抽象类,抽象类中包含了Application环境的一些函数,设计角度而言,Context仅提供某些功能, extends 才是类的本质,即 Activity 的本质是一个 Context ,其所实现的其余接口只是为了扩充 Context 的功能而已,扩充后的类称之为 Activity 或 Service函数
Context个数=1 + Activity个数 + Service个数this
在博客 ActivityThread.main过程 中分析中能够知道, handleBindApplication 函数中会调用 makeApplicationspa
makeApplication会建立Application以及建立 ContextImpl设计
ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
复制代码
这里的this是 LoadedApk 对象,该对象是在 handleBinderApplication 中赋值code
data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
复制代码
在该函数中,会根据 AppBindData(handleBinderApplication中的参数) 中的ApplicationInfo的mPackageName建立一个PackageInfo对象并保存为ActivityThread类的全局对象orm
显然,一个应用程序中全部Activity或者Application或Servie,他们的mPackageName是同样的,即为包名,所以ActivityThread只会有一个全局的PackageInfo对象对象
在 newApplication的函数中会调用 Application 的 attach
final void attach(Context context) {
attachBaseContext(context);
mLoadedApk = ContextImpl.getImpl(context).mPackageInfo;
}
复制代码
查看 attachBaseContext
protected void attachBaseContext(Context base) {
if (mBase != null) {
throw new IllegalStateException("Base context already set");
}
mBase = base;
}
复制代码
这个mBase就是 ContextWrapper 中的 Context
在 Launcher启动流程 的分析中能够知道,handleLaunchActivity 会调用到 performLaunchActivity,该函数会调用 createBaseContextForActivity
private ContextImpl createBaseContextForActivity(ActivityClientRecord r) {
final int displayId;
...
ContextImpl appContext = ContextImpl.createActivityContext(
this, r.packageInfo, r.activityInfo, r.token, displayId, r.overrideConfig);
...
return appContext;
}
复制代码
createActivityContext 中的 packageInfo 信息和上小节分析的流程基本一致,他也是全局的
static ContextImpl createActivityContext(ActivityThread mainThread, LoadedApk packageInfo, ActivityInfo activityInfo, IBinder activityToken, int displayId, Configuration overrideConfiguration) {
...
ContextImpl context = new ContextImpl(null, mainThread, packageInfo, activityInfo.splitName,
activityToken, null, 0, classLoader);
...
return context;
}
复制代码
建立Context完成后,调用 activity 的 attach 函数
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);
复制代码
attach 函数中作了不少的赋值操做,其中 attachBaseContext 的函数和Application的 attachBaseContext 中做用一致,把context赋值给 ContextWrapper 的 mBase
:::danger 笔记 所以,当咱们翻阅Activity源码,看到mBase时,就应该去找 ContextImpl 里的方法 :::
Service的启动和Activity相似,最终一样会调用到ActivityThread里的函数,为 scheduleCreateService,接着调用 handleCreateService
在 handleCreateService 中会建立Context
ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
context.setOuterContext(service);
Application app = packageInfo.makeApplication(false, mInstrumentation);
service.attach(context, this, data.info.name, data.token, app,
ActivityManager.getService());
service.onCreate();
复制代码
Context的建立方式和Application一致,一样在建立后会调用 attach 进行一些赋值操做,一样也有以前分析的 mBase
不一样Context子类中PackageInfo对象来源
类名 | 远程数据类 | 本地数据类 | 赋值方式 |
---|---|---|---|
Application | ApplicationInfo | AppBindData | getPackageInfoNoCheck |
Activity | ActivityInfo | ActivityClientRecord | getPackageInfo |
Service | ServiceInfo | CreateServiceData | getPackageInfoNoCheck |
参考书籍: Android内核剖析