1.添加依赖

implementation "me.leolin:ShortcutBadger:1.1.22@aar"

依赖github地址

https://github.com/leolin310148/ShortcutBadger

2.创建service

public class BadgeIntentService extends IntentService {    private int notificationId = 0;    public BadgeIntentService() {        super("BadgeIntentService");    }    private NotificationManager mNotificationManager;    @Override    public void onStart(Intent intent, int startId) {        super.onStart(intent, startId);        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);    }    @Override    protected void onHandleIntent(Intent intent) {        if (intent != null) {            int badgeCount = intent.getIntExtra("badgeCount", 0);            mNotificationManager.cancel(notificationId);            notificationId++;            Notification.Builder builder = new Notification.Builder(getApplicationContext())                    .setContentTitle("")                    .setContentText("")                    .setSmallIcon(R.mipmap.ic_launcher);            Notification notification = builder.build();            ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount);            mNotificationManager.notify(notificationId, notification);        }    }}

3.工具类

public class BadgeUtil {    public static void applyBadgeCount(Context context, int badgeCount) {        if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {            // 判断机型是否是小米            context.startService(new Intent(context, BadgeIntentService.class).putExtra("badgeCount", badgeCount));        } else {            ShortcutBadger.applyCount(context, badgeCount);        }    }    public static void removeBadgeCount(Context context) {        ShortcutBadger.removeCount(context);    }}

4.调用

BadgeUtil.applyBadgeCount(MainActivity.this, 10);//显示角标BadgeUtil.removeBadgeCount(MainActivity.this);//关闭角标

5.别忘了添加网络权限和声明service

 

更多相关文章

  1. 使用android 隐藏命令
  2. Android(安卓)解决Could not find com.android.tools.build:grad
  3. 【Android自学笔记】为Android应用程序添加Rate功能
  4. Android(安卓)调用系统相机拍照的返回结果
  5. android里,addContentView()动态增加view控件,并实现控件的顶部,中
  6. Android(安卓)简单的从本机获取相机功能 并且自动添加
  7. android 跳转到小米手机神隐模式
  8. 关于用eclipse开发android经常出现R异常问题
  9. android中去掉标题栏和状态栏

随机推荐

  1. ':app:transformClassesWithDexForDebug'
  2. Android锁屏监听
  3. android Edittext内容字体大小动态变化
  4. android 检查gps
  5. [Android] 备份手机上的超级终端、VIM
  6. Android使用SAX解析XML(4)
  7. Android聊天软件开发(基于网易云IM即时通
  8. Android(安卓)Camera
  9. Android中的ListView实现图片文字和按钮
  10. android 键盘关闭/显示