应用图标右上角消息数量的显示

BadgeUtil 是一个工具类,用于在不同品牌手机(如小米、索尼、三星、HTC、Nova、华为、ZUK)的应用启动图标上显示未读消息数。通过判断手机制造商并调用相应的方法,实现消息数的设置和清除。
摘要由CSDN通过智能技术生成
// 让app logo显示消息数量
public void showCount() {
NotificationManager mNotificationManager = (NotificationManager) this


.getSystemService(Context.NOTIFICATION_SERVICE);


Notification.Builder builder = new Notification.Builder(this);


// .setContentTitle("title").setContentText("text").setSmallIcon(R.drawable.point_on).setAutoCancel(true);;


Notification notification = builder.build();
try {


Field field = notification.getClass().getDeclaredField("extraNotification");


Object extraNotification = field.get(notification);


Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);


method.invoke(extraNotification, allCount);


} catch (Exception e) {


e.printStackTrace();


}
mNotificationManager.notify(0, notification);
BadgeUtil.setBadgeCount(notification, context, allCount);


}












/*
 * 应用启动图标未读消息数显示 工具类  (效果如:QQ、微信、未读短信 等应用图标)<br/>

*  这里包含了小米,sony,Samsung,htc,nova,huawei,zuk  全部亲测有效
 * */
public class BadgeUtil {
 /**
     * Set badge count<br/>
     * 针对 Samsung / xiaomi / sony 手机有效
     * @param context The context of the application package.
     * @param count Badge count to be set
     */
    public static void setBadgeCount(Notification notification,Context context, int count) {
        if (count <= 0) {
            count = 0;
        } else {
            count = Math.max(0, Math.min(count, 99));
        }


        if (Build.MANUFACTURER.equalsIgnoreCase("

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值