Toast WindowManager$BadTokenException

Toast 作為 Android 常用的控件之一,突然在友盟統(tǒng)計上發(fā)現(xiàn)Toast報錯了,那就需要麻溜的解決了。

復(fù)現(xiàn)

        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)

出現(xiàn)原因

API 25, Android 為Toast增加了一個IBinder windowToken 去處理Toast#handleShow(),從而導(dǎo)致了一個運行時錯誤:BadTokenException 。
坑的理由:
其他版本上不會出現(xiàn)。因為 google在該版本開始對TYPE TOAST進(jìn)行管控,防止一個應(yīng)用的懸浮窗一直懸浮在另一個應(yīng)用上造成干擾,但是Android團隊意識到這個崩潰問題,從而在API 26的時候,在Toast的內(nèi)部加了try-catch保護捕獲了這個錯誤。
目前只有7.1.1上面的Toast存在這個問題,崩潰在系統(tǒng)源碼里。APP層可以通過自定義Toast類,反射替換TN的內(nèi)部成員變量mHandler,從而添加try-catch做到workaround,所有使用Toast的地方都使用這個自定義的,不要直接使用系統(tǒng)原生的
具體源碼對比 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();
            }

        }
    }
}

參考:https://cloud.tencent.com/developer/article/1034225

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 先給出異常信息圖: 復(fù)現(xiàn)步驟:在7.1.1 復(fù)現(xiàn)的步驟是,在Toast.show()之后,阻塞了Handler m...
    Stay_Li閱讀 3,034評論 0 8
  • 前言:toast再常見不過,但是一個小小的toast居然內(nèi)有乾坤,呵(w)呵(t)呵(f) 源碼如下: publi...
    super超_9754閱讀 1,542評論 0 0
  • ¥開啟¥ 【iAPP實現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,335評論 0 17
  • 東西有點多,但是資源絕對nice,自己都全部親身體驗過了,大家可放心使用 github排名: https://gi...
    Rance935閱讀 11,639評論 26 312
  • >>往期目錄<< 12 秦川又一次帶著蔣夢嬋曠課了。這對于蔣夢嬋來說并沒有什么,她只需要一個理由,或是身體不適,或...
    王禪童閱讀 307評論 0 2

友情鏈接更多精彩內(nèi)容