Android 小米角标无法修改问题研究

参考文献:
https://blog.csdn.net/uyy203/article/details/70160752?locationNum=15&fps=1 (延迟修改角标)
https://github.com/leolin310148/ShortcutBadger (修改角标代码)
https://blog.csdn.net/kongbaidepao/article/details/48317813 (APP内无法修改角标)
在修改角标的时候,小米一直存在一个问题,我按照官网的代码去修改角标数量,你会发现第一次修改成功(比如你要显示10条),第二次修改失败(比如你要显示11条),但是你会发现它又默认会从1开始。

如果你在APP里面是无法修改角标的,你要在桌面才能修改角标

解决办法是针对小米系统增加延时的操作显示

     //延迟操作
 new Handler().postDelayed(new ShortCutRunnable(count,notification,notifId,notificationManager),600);
 /**
     *作者:GaoXiaoXiong
     *创建时间:2018/4/8
     *注释描述:延迟显示角标个数,解决小米不正确显示角标
     */
 class ShortCutRunnable implements Runnable{
        private int count;//要显示的个数
        private int notifId;//可以采用System.currentTimeMillis()
        private Notification notification;
        private NotificationManager notificationManager;
        public ShortCutRunnable(int count, Notification notification,int notifId,NotificationManager notificationManager) {
            this.count = count;
            this.notification = notification;
            this.notifId = notifId;
            this.notificationManager = notificationManager;
        }

        @Override
        public void run() {
            try {
                ShortCutBadgerCount.getShortCutBadgerCount().clearCount();//我自己定义的
                Field field = notification.getClass().getDeclaredField("extraNotification");
                Object extraNotification = field.get(notification);
                Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
                method.invoke(extraNotification, count);//显示个数
            } catch (Exception e) {
                BAFLogger.e(TAG,e.getMessage());
            }
            notificationManager.notify(notifId, notification);//显示通知
        }
    }

ShortCutBadgerCount.java

/**
 * 创建时间: 2018/4/4
 * gxx
 * 注释描述:角标个数
 */
public class ShortCutBadgerCount {
    private ShortCutBadgerCount() {
    }
    private int cutCount=0;
    private volatile static ShortCutBadgerCount shortCutBadgerCount;
    public static ShortCutBadgerCount getShortCutBadgerCount(){
        if (shortCutBadgerCount==null){
            synchronized (ShortCutBadgerCount.class){
                if (shortCutBadgerCount==null){
                    shortCutBadgerCount = new ShortCutBadgerCount();
                }
            }
        }
        return shortCutBadgerCount;
    }
    /**
     *作者:GaoXiaoXiong
     *创建时间:2018/4/4
     *注释描述:添加1个角标
     */
    public int addCount(){
        this.cutCount+=1;
        setCutCount(this.cutCount);
        return cutCount;
    }
    /**
     *作者:GaoXiaoXiong
     *创建时间:2018/4/4
     *注释描述:设置个数
     */
    public void setCutCount(int cutCount) {
        this.cutCount = cutCount;
    }
    /**
     *作者:GaoXiaoXiong
     *创建时间:2018/4/8
     *注释描述:消息个数
     */
    public int getCutCount() {
        return cutCount;
    }
    /**
     *作者:GaoXiaoXiong
     *创建时间:2018/4/4
     *注释描述:清零
     */
    public void clearCount(){
        cutCount = 0;
    }
    /**
     *作者:GaoXiaoXiong
     *创建时间:2018/4/8
     *注释描述:销毁
     */
    public void destory(){
        if (shortCutBadgerCount!=null){
            removeShortcutBadgerACount();
            shortCutBadgerCount=null;
        }
    }

    /**
     *作者:GaoXiaoXiong
     *创建时间:2018/4/8
     *注释描述:移除角标和清空统计的角标个数
     */
    public void removeShortcutBadgerACount(){
        ShortcutBadger.removeCount(ABSApplication.getApplication()); //移除桌面角标
        ShortCutBadgerCount.getShortCutBadgerCount().clearCount();//清除个数
    }
}
    原文作者:一个冬季
    原文地址: https://www.jianshu.com/p/8c16e17c6dab
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞