Android 实现应用桌面角标

您所在的位置:网站首页 android桌面图标文字多行显示 Android 实现应用桌面角标

Android 实现应用桌面角标

2023-09-05 23:55| 来源: 网络整理| 查看: 265

1、小米手机

官方文档:文档中心

/** * 小米手机创建通知信息并创建角标 * * @param context * @param num */ public static void setXiaoMiBadgeNum(Context context, int num) { Log.e(TAG, "--------setXiaoMiBadgeNum----------"); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); String title = "消息提示"; String desc = "您有" + num + "条未读消息"; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "default"; String channelName = "默认通知"; NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH); channel.setShowBadge(true); notificationManager.createNotificationChannel(channel); } Notification notification = new NotificationCompat.Builder(context, "default") .setContentTitle(title) .setContentText(desc) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setAutoCancel(true) .setChannelId("default") .setNumber(num)//桌面角标数量 .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL) .build(); //取消掉上一条通知消息 notificationManager.cancel(notificationId); try { Field field = notification.getClass().getDeclaredField("extraNotification"); Object extraNotification = field.get(notification); Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class); method.invoke(extraNotification, num); } catch (Exception e) { e.printStackTrace(); } notificationManager.notify(notificationId, notification); }

需要注意以下几点:

1)发送通知必须当app处于后台才会生成桌面角标,否则只会生成一次。

2)该方法目前支持到miui12.

2、三星手机

已在三星s7dege+上,Android8.0上通过

public static void setSamSungBadgeNum(Context context,int num){ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.mipmap.ic_launcher) .setWhen(System.currentTimeMillis()) .setAutoCancel(true); mBuilder.setContentTitle("test"); mBuilder.setTicker("test"); mBuilder.setContentText("test"); //点击set 后,app退到桌面等待3s看效果(有的launcher当app在前台设置未读数量无效) final Notification notification = mBuilder.build(); sendBadgeNotification(notification, notificationId, context, num, num); } /** * 重置、清除Badge未读显示数 * * @param context */ public static void resetBadgeCount(Context context) { sendBadgeNotification(null, 0, context, 0, 0); } public static void sendBadgeNotification(Notification notification, int notifyID, Context context, int thisNotifyCount, int count) { if (count


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3