public static void setBadgeNum(Context context, int number) {//context对象,小红点的数量
if (isEmui()) {//这个是华为的
//华为的如果不想有小红点,传0就可以
try {
Bundle bunlde = new Bundle();
bunlde.putString("package", context.getPackageName()); // 这个是包名
bunlde.putString("class", context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName()); // 这个是应用启动的页面路径
bunlde.putInt("badgenumber", number);
context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bunlde);
} catch (Exception e) {
LogUtil.error("华为桌面图标", e.getMessage());
}
} else if (isMiui()) {//这个是小米的
//小米的需要发一个通知,进入应用后红点会自动消失,测试的时候进程只能在后台,否则没有效果
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
String title = "这个是标题";
String desc = "这个是内容";
if (SystemUtil.hasO()){
String channelId = "default";
String channelName = "默认通知";
notificationManager.createNotificationChannel(new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH));
}
Intent intent = new Intent();//可以用intent设置点击通知后的页面
Notification notification = new NotificationCompat.Builder(context, "default")
.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0))
.setContentTitle(title)
.setContentText(desc)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.build();
try {
Field field = notification.getClass().getDeclaredField("extraNotification");
Object extraNotification = field.get(notification);
Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
method.invoke(extraNotification, number);
notificationManager.notify(66, notification);
} catch (Exception e) {
LogUtil.error("小米桌面图标", e.getMessage());
}
}
}
其他厂商的:
VIVO暂不支持;
OPPO则需要申请,比较麻烦;
魅族不支持,但若是需要,那么可以跳转到应用的通知管理页面,提示用户自动打开桌面角标
三星可能是可以,但没有三星手机,就没有测试,有需要的话就自己试试吧~~~