Android Logo消息角标数字提醒
16lz
2021-01-23
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
更多相关文章
- android 跳转到小米手机神隐模式
- android 自定义通知消息设置背景色不生效,导致部分机型显示白色字
- 小米,红米手机android 6.0以下 权限管理
- Android Launcher 如何去掉主菜单,所有应用摆在桌面,类似小米桌面
- android-【机型-版本-分辨率】测试点罗列
- 小米手机十月份会有原生 Android 2.3.5 供下载,明年一月推出 4.0
- Android 悬浮窗权限各机型各系统适配大全(总结)
- android获取手机型号和手机厂商
- Android获取手机型号,系统版本,App版本号等信息