ContextImpl—真正实现Context功能的类
class ContextImpl extends Context {
//整个App的主线程
final ActivityThread mMainThread;
//整个App的相关信息
final LoadedApk mPackageInfo;
//资源解析器
private final ResourcesManager mResourcesManager;
//App资源类
private final Resources mResources;
//外部Context的引用
private Context mOuterContext;
//默认主题.
private int mThemeResource = 0;
private Resources.Theme mTheme = null;
//包管理器
private PackageManager mPackageManager;
................................
//以下是静态区注册系统的各种服务,多大五六十种系统服务,因此每个持有Context引用的对象都可以随时通过getSystemService方法来轻松获取系统
服务。
static {
registerService(ACCESSIBILITY_SERVICE, new
ServiceFetcher() {
public Object getService(ContextImpl ctx)
{
return
AccessibilityManager.getInstance(ctx);
}});
registerService(CAPTIONING_SERVICE, new
ServiceFetcher() {
public Object getService(ContextImpl ctx)
{
return new CaptioningManager(ctx);
}});
registerService(ACCOUNT_SERVICE, new
ServiceFetcher() {
public Object createService(ContextImpl
ctx) {
IBinder b =
ServiceManager.getService(ACCOUNT_SERVICE);
IAccountManager service =
IAccountManager.Stub.asInterface(b);
return new AccountManager(ctx,
service);
}});
........................
}
.................
//启动Activity的地方
@Override
public void startActivity(Intent intent, Bundle
options) {
warnIfCallingFromSystemProcess();
if
((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
throw new AndroidRuntimeException(
"Calling startActivity() from outside
of an Activity "
+ " context requires the
FLAG_ACTIVITY_NEW_TASK flag."
+ " Is this really what you want?");
}
mMainThread.getInstrumentation().execStartActivity(
getOuterContext(),
mMainThread.getApplicationThread(), null,
(Activity)null, intent, -1, options);
}
..........
//启动服务的地方
@Override
public ComponentName startService(Intent service) {
warnIfCallingFromSystemProcess();
return startServiceCommon(service, mUser);
}
...............
}
LayoutInflater inflater = LayoutInflater.from(mContext)*;*
View layout =
inflater.inflate(R.layout.activity_main,null)*;*
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not
found.");
}
return LayoutInflater;
}
Context内存泄漏问题
总结 Context是什么?
原文:https://www.cnblogs.com/AronJudge/p/14647084.html