Toast 作为 Android 常用的控件之一,突然在友盟统计上发现Toast报错了,那就需要麻溜的解决了。
复现
Toast.makeText(getContext(), "我的Toast", Toast.LENGTH_SHORT).show();
try {
Thread.sleep(5_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
错误日志
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@a22690b is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:679)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.widget.Toast$TN.handleShow(Toast.java:459)
at android.widget.Toast$TN$2.handleMessage(Toast.java:342)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
出现原因
API 25, Android 为Toast增加了一个IBinder windowToken 去处理Toast#handleShow(),从而导致了一个运行时错误:BadTokenException 。
坑的理由:
其他版本上不会出现。因为 google在该版本开始对TYPE TOAST进行管控,防止一个应用的悬浮窗一直悬浮在另一个应用上造成干扰,但是Android团队意识到这个崩溃问题,从而在API 26的时候,在Toast的内部加了try-catch保护捕获了这个错误。
目前只有7.1.1上面的Toast存在这个问题,崩溃在系统源码里。APP层可以通过自定义Toast类,反射替换TN的内部成员变量mHandler,从而添加try-catch做到workaround,所有使用Toast的地方都使用这个自定义的,不要直接使用系统原生的
具体源码对比 Toast#TN#Handler:
API25:
public void handleShow(IBinder windowToken) {
if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView
+ " mNextView=" + mNextView);
if (mView != mNextView) {
...
...
if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this);
mWM.addView(mView, mParams);
trySendAccessibilityEvent();
}
}
API:26
public void handleShow(IBinder windowToken) {
if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView
+ " mNextView=" + mNextView);
// If a cancel/hide is pending - no need to show - at this point
// the window token is already invalid and no need to do any work.
if (mHandler.hasMessages(CANCEL) || mHandler.hasMessages(HIDE)) {
return;
}
if (mView != mNextView) {
...
...
try {
mWM.addView(mView, mParams);
trySendAccessibilityEvent();
} catch (WindowManager.BadTokenException e) {
/* ignore */
}
}
}
解决方案:
public class ToastCompat {
private static Field sField_TN;
private static Field sField_TN_Handler;
static {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
try {
sField_TN = Toast.class.getDeclaredField("mTN");
sField_TN.setAccessible(true);
sField_TN_Handler = sField_TN.getType().getDeclaredField("mHandler");
sField_TN_Handler.setAccessible(true);
} catch (Exception e) {
}
}
}
private static void hook(Toast toast) {
try {
Object tn = sField_TN.get(toast);
Handler preHandler = (Handler) sField_TN_Handler.get(tn);
sField_TN_Handler.set(tn, new SafelyHandlerWarpper(preHandler));
} catch (Exception e) {
}
}
public static void showToast(Context context, CharSequence cs, int length) {
Toast toast = Toast.makeText(context, cs, length);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
hook(toast);
}
toast.show();
}
private static class SafelyHandlerWarpper extends Handler {
private Handler impl;
public SafelyHandlerWarpper(Handler impl) {
this.impl = impl;
}
@Override
public void handleMessage(Message msg) {
try {
impl.handleMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}