Toast弹不出来之谜

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33505109/article/details/85001074

前言

今天早上测试应用的时候,忽然发现Toast弹不出来了,我用的华为测试机,以为是通知权限被关了,后来发现是开着的,这就纳了闷了,这个Toast工具类用了好长时间了,后来发现这Toast原来还能这样...

正文

以前工具类是这个样子的(华为的通知权限放在了其他的地方)

final public class ToastUtil {
    private static Toast toast;//单例的toast

    /**
     * 显示Toast
     */
    public static void showToast(int text) {
        showToast(App.getInstance().getResources().getString(text));
    }

    public static void showToast(String text) {
        if (toast == null)
            toast = Toast.makeText(App.getInstance(), text, Toast.LENGTH_SHORT);
        toast.setText(text);
        toast.show();
    }
}

平时用的都是好好的,为什么不能用了呢?

然后我通过debug发现所有路径都走了,说明是调用成功了,后来想起来,是不是子线程的问题,打印了一下线程信息:

Thread.currentThread().getName();

发现果然第一次调用没有跑在主线程中,这就尴尬了,在子线程中创建,并show()竟然没有报错,而且子线程创建的在主线程调用show()也没有报错,只是不显示

到show()的源码中发现,被try了..

    /**
     * Show the view for the specified duration.
     */
    public void show() {
        if (mNextView == null) {
            throw new RuntimeException("setView must have been called");
        }

        INotificationManager service = getService();
        String pkg = mContext.getOpPackageName();
        TN tn = mTN;
        tn.mNextView = mNextView;

        try {
            service.enqueueToast(pkg, tn, mDuration);
        } catch (RemoteException e) {
            // Empty
        }
    }

修改方法

在创建和show的时候可以判断是否是主线程,如果不是就放到主线程中就ok了

修改后的代码,嗯,这样就可以了(虽然有强迫症看到这代码很不爽,但是首先得能用)

final public class ToastUtil {
    private static Toast toast;//单例的toast

    /**
     * 显示Toast
     */
    public static void showToast(int text) {
        showToast(App.getInstance().getResources().getString(text));
    }

    public static void showToast(final String text) {
        if (toast == null) {
                AppManager.getAppManager().currentActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toast = Toast.makeText(App.getInstance(), text, Toast.LENGTH_SHORT);
                        toast.show();
                    }
                });
        } else {
            //如果show()是在子线程触发的,则在主线程来显示
            if ("main".equals(Thread.currentThread().getName())) {
                toast.setText(text);//将文本设置给toast
                toast.show();
            } else {
                AppManager.getAppManager().currentActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toast.setText(text);//将文本设置给toast
                        toast.show();
                    }
                });
            }
        }
    }
}

end

猜你喜欢

转载自blog.csdn.net/qq_33505109/article/details/85001074