美文网首页
Android 9.0 Toast 重复显示问题

Android 9.0 Toast 重复显示问题

作者: ncd | 来源:发表于2020-01-14 21:04 被阅读0次

    在android 低版本上保持提个toast实例即可解决重复显示问题,这个对普通app和系统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代码已经改变,系统对这个问题进行了优化普通应用解决方法就是直接适用Toast.makeText(...).show()去显示,但是我遇到系统app(android.uid.system)使用单例时前两个还能显示,后面的就直接消失不见了,使用原生方法还是会显示很久,不是说系统已经优化了吗?
    看了源码发现系统对应用进行了区分,系统应用和非系统应用走的不是一个方法:
    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,否则就会使用队列里已经有的,就是这样达到优化的目的。我加上log运行发现我的应用每次都是-1,普通app就第一次是-1.后面都是0。所以每次都是重新创建的record.

    为了尽量少修改源码,在应用中做特殊处理,每次显示时把原来的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();
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Android 9.0 Toast 重复显示问题

          本文链接:https://www.haomeiwen.com/subject/ekzaactx.html