Android 实现华为,小米的桌面角标(小红点)

华为与小米桌面小红点数设置
本文详细介绍了如何在华为和小米设备上设置应用图标的小红点数量,包括通过调用特定API和发送通知的方式实现。对于不同品牌的手机,如VIVO、OPPO、魅族和三星,也提到了其支持情况及可能的解决方案。
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则需要申请,比较麻烦;
魅族不支持,但若是需要,那么可以跳转到应用的通知管理页面,提示用户自动打开桌面角标
三星可能是可以,但没有三星手机,就没有测试,有需要的话就自己试试吧~~~

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值