在项目中,有一个重要的业务模块,需要将Android设备上的部分界面显示到扩展屏幕上,例如新接入的显示器,投影仪等。Android提供了一个Presentation的API接口能够实现该需求,使用起来也非常简单,只需要构建一个集成Presentation接口的一个实体类,然后show出来就行了,其实Presentation继承自Dialog,它具有Dialog所有的特性,这样理解就很简单了,就是将一个Dialog弹出到指定显示设备上就好了。功能很快实现了,并且看起来还不错,可是在提交给测试后,测试人员在测试反复插拔显示设备和Android设备的连接线时,出现了如下崩溃异常:
java.lang.IllegalArgumentException:View=com.android.internal.policy.impl.PhoneWindow$DecorView{4204b710 V.E.....R.....I. 0,0-1920,1080} not attached to window manager
atandroid.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)
atandroid.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
at android.app.Dialog.dismissDialog(Dialog.java:329)
at android.app.Dialog.dismiss(Dialog.java:312)
at android.app.Dialog.cancel(Dialog.java:1114)
at android.app.Presentation.handleDisplayRemoved(Presentation.java:265)
at android.app.Presentation.access$100(Presentation.java:141)
at android.app.Presentation$2.onDisplayRemoved(Presentation.java:331)
atandroid.hardware.display.DisplayManagerGlobal$DisplayListenerDelegate.handleMessage(DisplayManagerGlobal.java:455)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
通过查资料后,分析了一下原因,应该是因为外接显示设备的连接线在拔出时正好处于Presentation窗口所依赖的Activity处于关闭流程,所以导致not attached to window manager的异常错误,然后通过如下两种方式进行了改善:
1. 注册外接显示设备的插拔监听,并在监听到移除事件时将Presentation窗口dismiss
displayManager = (DisplayManager)UiApplication.getInstance().getSystemService(Context.DISPLAY_SERVICE);
displayManager.registerDisplayListener(newDisplayListener()
{
@Override
public void onDisplayRemoved(int displayId) {
if(presentation != null) {
presentation.dismiss();
}
......
}
@Override
public void onDisplayChanged(int displayId) {
......
}
@Override
public void onDisplayAdded(int displayId) {
......
}
}, null)
2. 保证Presentation生命周期在Activity内部,提前将presentation关掉。
@Override
protected void onStop()
{
super.onStop();
try
{
if (UiApplication.presentation != null)
{
UiApplication.presentation.cancel();
UiApplication.presentation = null;
}
}
catch (Exception e)
{
Log.exception(TAG, e);
}
}
经过以上改动后,确实有段时间没有出现过问题,正在以为问题已经修复时候,又出现了上面的崩溃现象,简直无法想象。经过仔细看代码逻辑,找不到问题出在哪里,在网上找了许久,没有相关的信息。仔细跟了一下源码后,发现了一些以前遗漏的问题:
1. 其实Android本身已经处理了外接显示设备插拔事件,我们根本不需要特殊处理,当连接线拔掉时,系统会先通知设备移除,然后自动调用Presentation的cancel方法。
/**
* Called by the system when the properties ofthe {@link Display} to which
* the presentation is attached have changed.
*
* If the display metrics have changed (forexample, if the display has been
* resized or rotated), then the systemautomatically calls
* {@link #cancel} to dismiss the presentation.
*
* @see #getDisplay
*/
publicvoid onDisplayChanged() {
}
privatevoid handleDisplayRemoved() {
onDisplayRemoved();
cancel();
}
2. 既然系统自动调用cancel方法,所以之前为了防止Presentation生命周期超出其所在Activity生命周期的尝试其实是没有特殊意义的。
既然这样,貌似无法解决这种人为频繁插拔连接线导致Android处理异常,可是我觉得既然异常是在cancel方法中抛出的,是否可以通过重载一下Presentation的cancel方法,将该异常捕获掉,至少保证APP不会崩溃。
@Override
public void cancel()
{
try
{
super.cancel();
}
catch (Exception e)
{
Log.exception(TAG, e);
}
}
通过如上的处理后,再也没有出现过插拔连接线导致的异常崩溃,感觉这种修改确实有效,但是这种walk around的方法不一定是最好的,还希望有人能提出更好的解决方案,烦请分享,谢谢。