如果你在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();//清除个数
}
}