Android Logo消息角标数字提醒_第1张图片

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 自定义通知消息设置背景色不生效,导致部分机型显示白色字
  3. 小米,红米手机android 6.0以下 权限管理
  4. Android Launcher 如何去掉主菜单,所有应用摆在桌面,类似小米桌面
  5. android-【机型-版本-分辨率】测试点罗列
  6. 小米手机十月份会有原生 Android 2.3.5 供下载,明年一月推出 4.0
  7. Android 悬浮窗权限各机型各系统适配大全(总结)
  8. android获取手机型号和手机厂商
  9. Android获取手机型号,系统版本,App版本号等信息

随机推荐

  1. Android(安卓)P WiFi 连接以及获取 IP 流
  2. Android(安卓)中日期对话框的应用
  3. 【高通SDM660平台 Android(安卓)10.0】(1
  4. android如何让webview里的资源访问本地缓
  5. android相对布局实现梅花效果
  6. Android文件的读写
  7. Android(安卓)studio APP运行异常集锦
  8. android触摸震动原理
  9. Android中进程相关信息获取,pid,进程名
  10. android 消息传递机制