Skip to content

ToastCompat for Android 7.1.1, Avoiding Toast BadTokenException

Notifications You must be signed in to change notification settings

xtf115/ToastCompat

 
 

Repository files navigation

ToastCompat

ToastCompat for Android 7.1.1, Avoiding Toast BadTokenException , Just what exactly the repo ToastCompat do. I name this repo "ToastCompat" just want somebody could be help.

现在你看到的这个repo,是比另外一个ToastCompat,手机兼容性更加广的方案

Bug Fixed

2018-01-30 these patch did not work on some devices (OPPO CPH1707)(OPPO CPH1707使用了界于6.0与7.1.1之间的代码,真是辛苦他们合并代码了..)

Why

for more details about BadTokenException on Toast

There is ToastCompat do the same thing , why i create this?

ToastCompat use a windowmanager wrapper to hook the system windowmanager. It is a great solution to fix the bug. But when i test in the phone( Nubia Z17S),the Toast can't be show. I can't find the reason for it, It is weird,but it do happen. I had reported a bug to the author.

So i have to find another solution to fix the bug BadTokenException

Solution

Just copy ToastCompat.java to your project,and use it like Toast.

these is the main code to solve the issue:

    @Override
    public void show() {
        if(checkIfNeedToHack()){
            tryToHack();
        }
        super.show();
    }

    protected boolean checkIfNeedToHack(){
        return Build.VERSION.SDK_INT==Build.VERSION_CODES.N_MR1;
    }

    private void tryToHack(){
        try {
            Object mTN=getFieldValue(this,"mTN");
            if(mTN!=null){
                Object rawHandler=getFieldValue(mTN,"mHandler");
                if(rawHandler!=null){
                    setFieldValue(rawHandler,"mCallback",new InternalHandlerCallback((Handler)rawHandler));
                }
            }
        }catch (Throwable e){
            e.printStackTrace();
        }
    }

    private class InternalHandlerCallback implements Handler.Callback{
        private final Handler mHandler;

        public InternalHandlerCallback(Handler mHandler) {
            this.mHandler = mHandler;
        }

        @Override
        public boolean handleMessage(Message msg) {
            try {
                mHandler.handleMessage(msg);
            }catch (Throwable e) {
               e.printStackTrace();
            }
            return true;
        }
    }

About

ToastCompat for Android 7.1.1, Avoiding Toast BadTokenException

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%