你真的了解Context吗

引言

很多人应该知道Activity,Service中的Context和ApplicationContext的区别,而且也知道Context,ContextImpl,ContextWrapper,Activity,Service,Application构成的体系,在异步任务需要Context时,也知道为了防止内存泄露需要传递ApplicationContext而不是Activity的Context,但是这样的场景并不万能,因为并不是所有需要Activity的Context的地方都可以用ApplicationContext来代替。

1.Context继承体系

所以一个App进程中Context数量=Activity数量+Service数量+1.

2.Activity的startActivity()和Application的startActivity()的区别

Application.startActivity()本质上是调用其中的mBase(ContextImpl对象)的startActivity()方法,而ContextImpl.startActivity()如下:

1
2
3
4
5
6
7
8
9
10
11
12
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);
}

可以看到这里对Intent的flag进行了检查,如果没有FLAG_ACTIVITY_NEW_TASK属性,就会抛出异常。
那为什么Activity中就不需要做这样的检查了?
根本原因在于Application中的Context并没有所谓的任务栈,所以待启动的Activity就找不到task了,这样的话要启动Activity就必须将它放到一个新的task中,即使用singleTask的方式启动。

3.Dialog的创建

如下示例:

1
2
AlertDialog imageDialog = new AlertDialog.Builder(context).setTitle("状态操作").setItems(items, listener).create();
imageDialog.show();

如果其中的context是Application Context,那么会抛出以下异常:

1
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

这个异常是在ViewRoogImpl的setView()中抛出的。
而抛出这个异常的原因是与WMS进行Binder IPC(res=mWindowSession.addToDisplay())的结果,而这个结果是执行WMS中addWindow()的结果,该方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public int addWindow(Session session,IWindow client,int seq,WindowManager.LayoutParams attrs,int viewVisibility,int displayId,Rect outContentInsets, InputChannel outInputChannel){
if(token==null){
...
} else if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
AppWindowToken atoken = token.appWindowToken;
if (atoken == null) {
Slog.w(TAG, "Attempted to add window with non-application token "
+ token + ". Aborting.");
return WindowManagerGlobal.ADD_NOT_APP_TOKEN;
}
...
}
}

显然,这是由于AppWindowToken==null导致的,这个AppWindowToken对应client端中Window的IBinder mAppToken这个成员。

由于AlertDialog中的super()会调用Dialog的构造方法,所以我们先看一下Dialog的构造方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Dialog(Context context, int theme, boolean createContextThemeWrapper) {
if (createContextThemeWrapper) {
if (theme == 0) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(com.android.internal.R.attr.dialogTheme,
outValue, true);
theme = outValue.resourceId;
}
mContext = new ContextThemeWrapper(context, theme);
} else {
mContext = context;
}
mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Window w = PolicyManager.makeNewWindow(mContext);
mWindow = w;
w.setCallback(this);
w.setOnWindowDismissedCallback(this);
w.setWindowManager(mWindowManager, null, null);
w.setGravity(Gravity.CENTER);
mListenersHandler = new ListenersHandler(this);
}

注意其中的w.setWindowManager(),显然,传递的appToken为null.这也是Dialog和Activity窗口的一个区别,Activity会将这个appToken设置为ActivityThread传过来的token.

在Dialog的show()方法中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public void show() {
if (mShowing) {
if (mDecor != null) {
if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR);
}
mDecor.setVisibility(View.VISIBLE);
}
return;
}
mCanceled = false;
if (!mCreated) {
dispatchOnCreate(null);
}
onStart();
mDecor = mWindow.getDecorView();
if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
final ApplicationInfo info = mContext.getApplicationInfo();
mWindow.setDefaultIcon(info.icon);
mWindow.setDefaultLogo(info.logo);
mActionBar = new WindowDecorActionBar(this);
}
WindowManager.LayoutParams l = mWindow.getAttributes();
if ((l.softInputMode
& WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) {
WindowManager.LayoutParams nl = new WindowManager.LayoutParams();
nl.copyFrom(l);
nl.softInputMode |=
WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
l = nl;
}
try {
mWindowManager.addView(mDecor, l);
mShowing = true;
sendShowMessage();
} finally {
}
}

其中mWindow是PhoneWindow类型,mWindow.getAttributes()获取到的Type为TYPE_APPLICATION.

Dialog也是通过WindowManager把自己的Window添加到WMS上,但是这里在addView()之前,mWindow的token为null(前面已经分析了,w.setWindowManager的第二个参数为null).而WMS要求TYPE_APPLICATION的窗口的token不能为null.

而如果用Application或者Service的Context区获取这个WindowManager服务的话,会得到一个WindowManagerImpl对象,这个实例中token也是空的。

那为什么Activity就可以呢?
原来是Activity重写了getSystemService()方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Override
public Object getSystemService(@ServiceName @NonNull String name) {
if (getBaseContext() == null) {
throw new IllegalStateException("System services not available to Activities before onCreate()");
}
if (WINDOW_SERVICE.equals(name)) {
return mWindowManager;
} else if (SEARCH_SERVICE.equals(name)) {
ensureSearchManager();
return mSearchManager;
}
return super.getSystemService(name);
}

显然,对于WINDOW_SERVICE,返回的是mWindowManager对象,而这个对象的创建是在Activity的attach()方法中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
final void attach(Context context, ActivityThread aThread, Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances, Configuration config,
IVoiceInteractor voiceInteractor) {
attachBaseContext(context);
mFragments.attachActivity(this, mContainer, null);
mWindow = PolicyManager.makeNewWindow(this);
mWindow.setCallback(this);
mWindow.setOnWindowDismissedCallback(this);
mWindow.getLayoutInflater().setPrivateFactory(this);
if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
mWindow.setSoftInputMode(info.softInputMode);
}
if (info.uiOptions != 0) {
mWindow.setUiOptions(info.uiOptions);
}
mUiThread = Thread.currentThread();
mMainThread = aThread;
mInstrumentation = instr;
mToken = token;
mIdent = ident;
mApplication = application;
mIntent = intent;
mComponent = intent.getComponent();
mActivityInfo = info;
mTitle = title;
mParent = parent;
mEmbeddedID = id;
mLastNonConfigurationInstances = lastNonConfigurationInstances;
if (voiceInteractor != null) {
if (lastNonConfigurationInstances != null) {
mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
} else {
mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this, Looper.myLooper());
}
}
mWindow.setWindowManager((WindowManager) context.getSystemService(Context.WINDOW_SERVICE), mToken,
mComponent.flattenToString(), (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
mWindowManager = mWindow.getWindowManager();
mCurrentConfig = config;
}

注意其中的mWindow.setWindowManager(),在这里将Activity的mToken给了mWindow,所以这就是Activity中的mWindow和Dialog中的mWindow的区别。

所以不能通过Application和Service的Context来创建Dialog,只能通过Activity的Context来创建Dialog.