Android实现应用数字角标攻击力

本文介绍了Android应用数字角标的实现,详细讨论了华为、小米、vivo、OPPO、三星等主流厂商的支持情况及适配注意事项。针对小米、vivo和OPPO,特别提到了角标与通知的关系以及申请和配置过程。同时,提供了角标设置类、角标管理类的相关代码,帮助开发者进行设备兼容性处理。
摘要由CSDN通过智能技术生成

1、先大致说下主流厂商目前对角标的支持吧

(以下方案除、索尼、htc 、联想未验证过、其余都有验证。)

华为:支持、
小米:支持、
vivo:中高端支持、老版本不支持(7.0、8.0设备)、
oppo:、需要申请:【应用服务】教你如何给应用软件申请Push角标功能 - 应用 - 开发者社区
三星:支持、


2、小米、vivo oppo 适配注意的点

先说小米:小米角标和通知相关、即通知数量==角标显示数量、官方似乎也意识到了此设计的缺陷、提出了解决方案:文档中心
通知消失/app重启 角标都会消失。
oppo 申请完成后、默认开启桌面数字角标;
oppo(老设备):同小米
小米 vivo oppo(老设备):需要在通知权限中、开启圆角通知(不同设备叫法不一样、注意区分)。


3、相关代码

3-1、角标设置类(核心)

public class BadgerUtils {
   
 
    public static int notificationId = 100;
    //xiaomi oppo 通用 
    public static void setNotificationBadge(Context context, int count) {
   
        NotificationManager notificationManager = (NotificationManager) context.getSystemService
                (Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
   
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
   
            // 8.0之后添加角标需要NotificationChannel
            NotificationChannel channel = new NotificationChannel("badge", "齐齐乐通知角标",
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.setShowBadge(true);
            channel.enableLights(false);
            channel.enableVibration(false);
            channel.setSound(null, null);
            notificationManager.createNotificationChannel(channel);
        }
        if (!(context instanceof Activity)) {
   
            Log.d("BadgerUtils", "xiaomi or oppo not set On MainActivity、it's wrong");
            return;
        }
        String content=count==0?"暂无通知":"您有" + count + "条未读消息";
        Activity activity = (Activity) context;
        Intent intent = new Intent(context, activity.getClass());
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(context, "badge")
                .setContentTitle("齐齐乐")
                .setContentText(content)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap
                        .ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setChannelId("badge")
                .setNumber(count)
                .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL).build();
        // 小米
        try {
   
            Field field = notification.getClass().getDeclaredField("extraNotification");
            Object extraNotification = field.get(notification);
            Method method = extraNotification.getClass().getDeclaredMethod("extraNotification", int.class);
            method.invoke(extraNotification, count);
        } catch (Exception e) {
   
            e.printStackTrace();
        }
        notificationManager.notify(notificationId++, notification);
    }
 
 
    //华为
    public static boolean setHuaweiBadge(Context context, int count) {
   
        try {
   
            String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
            Bundle bundle = new Bundle();
            bundle.putString("package", context.getPackageName());
            bundle.putString("class", launchClassName);
            bundle.putInt("badgenumber", count);
            context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher" +
                    ".settings/badge/"), "change_badge", null, bundle);
            return true;
        } catch (Exception e) {
   
            e.printStackTrace();
            return false;
        }
    }
     
    //vivo
    public static void setVivoBadge(Context context, int count) {
   
        try {
   
            String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
            Intent intent = new Intent();
            intent.setAction("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
            intent.putExtra("packageName", context.getPackageName());
            intent.putExtra("className", launchClassName);
            intent.putExtra("notificationNum", count);
           /* if (Build.VERSION.SDK_INT>=26) {
   
                intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
            }*/
            context.sendBroadcast(intent);
        } catch (Exception e) {
   
            e.printStackTrace();
        }
    }
 
    //三星
    public static void setSamsungBadge(Context context, int count) {
   
        try {
   
            String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
            Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
            intent.putExtra("badge_count", count);
            intent.putExtra("badge_count_package_name", context.getPackageName());
            intent.putExtra("badge_count_class_name", launchClassName);
            context.sendBroadcast(intent);
        } catch (Exception e) {
   
            e.printStackTrace();
        }
 
    }
 
    //    联想ZUK(支持)
    public static void setZukBadge(Context context, int count) {
   
        try {
   
            Bundle extra = new Bundle();
            ArrayList<String> ids = new ArrayList<>();
            // 以列表形式传递快捷方式id,可以添加多个快捷方式id
//        ids.add("custom_id_1");
//        ids.add("custom_id_2");
            extra.putStringArrayList("app_shortcut_custom_id", ids);
            extra.putInt("app_badge_count", count);
            Uri contentUri = Uri.parse("content://com.android.badge/badge");
            Bundle bundle = context.getContentResolver().call(contentUri, "setAppBadgeCount", null,
                    extra);
        } catch (Exception e) {
   
            e.printStackTrace(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值