Android 9.0 Toast 重復(fù)顯示問題

在android 低版本上保持提個toast實例即可解決重復(fù)顯示問題,這個對普通app和系統(tǒng)app都適用

if (null == toast) {
    toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
} else {
    toast.setText(id);
    toast.setDuration(Toast.LENGTH_SHORT);
}
toast.show();

但是到了android 9.0,這樣就不太適用,因為Toast代碼已經(jīng)改變,系統(tǒng)對這個問題進行了優(yōu)化普通應(yīng)用解決方法就是直接適用Toast.makeText(...).show()去顯示,但是我遇到系統(tǒng)app(android.uid.system)使用單例時前兩個還能顯示,后面的就直接消失不見了,使用原生方法還是會顯示很久,不是說系統(tǒng)已經(jīng)優(yōu)化了嗎?
看了源碼發(fā)現(xiàn)系統(tǒng)對應(yīng)用進行了區(qū)分,系統(tǒng)應(yīng)用和非系統(tǒng)應(yīng)用走的不是一個方法:
NotificationManagerService.enqueueToast方法中

...
final boolean isSystemToast = isCallerSystemOrPhone() || ("android".equals(pkg));
final boolean isPackageSuspended =
                    isPackageSuspendedForUser(pkg, Binder.getCallingUid());
...
 synchronized (mToastQueue) {
                int callingPid = Binder.getCallingPid();
                long callingId = Binder.clearCallingIdentity();
                try {
                    ToastRecord record;
                    int index;
                    Slog.i(TAG, "isSystemToast:" + isSystemToast + ",isPackageSuspended:" + isPackageSuspended);
                    // All packages aside from the android package can enqueue one toast at a time
                    if (!isSystemToast) {
                        index = indexOfToastPackageLocked(pkg);
                    } else {
                        index = indexOfToastLocked(pkg, callback);
                    }

                    // If the package already has a toast, we update its toast
                    // in the queue, we don't move it to the end of the queue.
                    Slog.i(TAG, "index:" + index);
                    if (index >= 0) {
                        record = mToastQueue.get(index);
                        record.update(duration);
                        try {
                            record.callback.hide();
                        } catch (RemoteException e) {
                        }
                        record.update(callback);
                    } else {
                        Binder token = new Binder();
                        mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, DEFAULT_DISPLAY);
                        record = new ToastRecord(callingPid, pkg, callback, duration, token);
                        mToastQueue.add(record);
                        index = mToastQueue.size() - 1;
                    }
                    keepProcessAliveIfNeededLocked(callingPid);
                    // If it's at index 0, it's the current toast.  It doesn't matter if it's
                    // new or just been updated.  Call back and tell it to show itself.
                    // If the callback fails, this will remove it from the list, so don't
                    // assume that it's valid after this.
                    if (index == 0) {
                        showNextToastLocked();
                    }
                } finally {
                    Binder.restoreCallingIdentity(callingId);
                }
            }

其中index小于0會新建一個record,否則就會使用隊列里已經(jīng)有的,就是這樣達到優(yōu)化的目的。我加上log運行發(fā)現(xiàn)我的應(yīng)用每次都是-1,普通app就第一次是-1.后面都是0。所以每次都是重新創(chuàng)建的record.

為了盡量少修改源碼,在應(yīng)用中做特殊處理,每次顯示時把原來的taost取消再顯示,這樣雖然還是新建record,但是也能縮短顯示時間

public static void show(Context context, int id) {
        if (null == context)
            return;
        mContext = context.getApplicationContext();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            if (null == toast) {
                toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
            } else {
                toast.setText(id);
                toast.setDuration(Toast.LENGTH_SHORT);
            }
            toast.show();
        } else {
            if (null != toast) {
                toast.cancel();
                toast = null;
            }
            toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

完整代碼:

import android.content.Context;
import android.os.Build;
import android.widget.Toast;

import 你的Application;


public class ToastUtils {
    private static Toast toast;
    private static Context mContext;

    public static void show(String text, int duration) {
        show(MyApplication.getApplication().getApplicationContext(), text, duration);
    }

    public static void show(String text) {
        show(MyApplication.getApplication().getApplicationContext(), text);
    }

    public static void show(int id, int duration) {
        show(MyApplication.getApplication().getApplicationContext(), id, duration);
    }

    public static void show(int id) {
        show(MyApplication.getApplication().getApplicationContext(), id);
    }

    public static void show(Context context, String text, int duration) {
        if (null == context)
            return;
        mContext = context.getApplicationContext();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            if (null == toast) {
                toast = Toast.makeText(mContext, text, duration);
            } else {
                toast.setText(text);
                toast.setDuration(duration);
            }
            toast.show();
        } else {
            if (null != toast) {
                toast.cancel();
                toast = null;
            }
            toast = Toast.makeText(mContext, text, duration);
            toast.show();
        }

    }

    public static void show(Context context, String text) {
        if (null == context)
            return;
        mContext = context.getApplicationContext();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            if (null == toast) {
                toast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
            } else {
                toast.setText(text);
                toast.setDuration(Toast.LENGTH_SHORT);
            }
            toast.show();
        } else {
            if (null != toast) {
                toast.cancel();
                toast = null;
            }
            toast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
            toast.show();
        }

    }

    public static void show(Context context, int id, int duration) {
        if (null == context)
            return;
        mContext = context.getApplicationContext();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            if (null == toast) {
                toast = Toast.makeText(mContext, id, duration);
            } else {
                toast.setText(id);
                toast.setDuration(duration);
            }
            toast.show();
        } else {
            if (null != toast) {
                toast.cancel();
                toast = null;
            }
            toast = Toast.makeText(mContext, id, duration);
            toast.show();
        }

    }

    public static void show(Context context, int id) {
        if (null == context)
            return;
        mContext = context.getApplicationContext();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            if (null == toast) {
                toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
            } else {
                toast.setText(id);
                toast.setDuration(Toast.LENGTH_SHORT);
            }
            toast.show();
        } else {
            if (null != toast) {
                toast.cancel();
                toast = null;
            }
            toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

最后編輯于
?著作權(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)容

  • 問題描述 在Android開發(fā)中,Toast的重復(fù)顯示問題很早就有人提出了解決方案,具體做法就是全局使用一個Toa...
    飄逸解構(gòu)閱讀 2,108評論 5 24
  • 1、Toast概念和問題的引出 Toast 中文名"土司",應(yīng)該算是 Android 使用頻率很高的一個 widg...
    未見哥哥閱讀 836評論 0 5
  • 什么是土司(Toast)? Toast是Android系統(tǒng)提供的一種非常好的提示方式,在程序中可以使用它將一些短小...
    一航j(luò)ason閱讀 1,113評論 0 1
  • 如果把鮮花比做四季 那么 潔白淡雅的梔子是春 鮮活熱情的雛菊是夏 豐姿綽約的玫瑰是秋 孤獨矜寡的鳶尾是冬 如果把校...
    殷半芹閱讀 370評論 1 5
  • 勾搭宋體字【DAY1】宋體字書寫,上下頂格,左右碰壁,提、捺垂直書寫,撇、點水平書寫。 自評:宋體字比較容易臨摹,...
    若谷SS閱讀 2,003評論 0 2

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