Android角标开发Badge概述

所谓Badge,原本是IOS上的一个效果,指的是当手机接收到推送消息后,会在手机桌面对应的App图标的右上角显示一个未读消息数的提示。

而在Android系统中,手机桌面的管理是在系统应用Launcher上实现的,然而在原生的Android系统中,Launcher是不提供该功能效果的。但是,由于Android的AOSP(Android开放源码项目)代码能够被各大ROM厂商修改,所以很多被修改的ROM都可以支持这种Badge效果。

基本原理

都是监听App发出的广播,然后根据广播内容进行Launcher层面的快捷方式的修改。

难点

由于没有Android原生系统的支持,所以ROM厂商的实现方式很不统一,基本都是自己做自己的。因此,关键是要找到这些Launcher修改的广播(厂家提供的私有API)。

不同厂商API

  • 三星

    String launcherClassName = getLauncherClassName(context);
    if (launcherClassName == null) {
    return;
    }
    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", launcherClassName);
    context.sendBroadcast(intent);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 华为

    String launcherClassName = getLauncherClassName(context);
    if (launcherClassName != null) {
    Bundle extra = new Bundle();
    extra.putString("package", ct.getPackageName());
    extra.putString("class", launcherClassName);
    extra.putInt("badgenumber", count);
    context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, extra);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • MIUI

    • MIUI6以下
    Intent intent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
    intent.putExtra("android.intent.action.APPLICATION_MESSAGE_UPDATE","your app packageName/.LAUNCHER ActivityName");
    intent.putExtra(EXTRA_UPDATE_APPLICATION_MESSAGE_TEXT, unreadCount);
    context.sendBroadcast(intent);
    • 1
    • 2
    • 3
    • 4

    • MIUI6以上
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("小米角标")
                    .setContentText("miui桌面角标消息");
    
            NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
            Notification notification = builder.build();
            try {
                Object miuiNotification = Class.forName("android.app.MiuiNotification").newInstance();
                Field field = miuiNotification.getClass().getDeclaredField("messageCount");
                field.setAccessible(true);
                field.set(miuiNotification, Integer.valueOf(count));
                notification.getClass().getField("extraNotification").set(notification, miuiNotification);
                managerCompat.notify(0, notification);
    
            }  catch (Exception e) {
                e.printStackTrace();
            }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

  • Sony

    Intent intent = new Intent();
    String launcherclassname = getLauncherClassName(context);
    if (launcherclassname != null) {
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", unReadCount > 0);
    intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherclassname);
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", unReadCount < 1 ? "" : unReadCount);
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
    context.sendBroadcast(intent);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • VIVO

    String launcherclassname = getLauncherClassName(context);
    if (launcherclassname != null) {
    Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
    //Constants.KEY_PKG_NAME = "packageName"
    //Constants.KEY_CLASS_NAME = "className"
    intent.putExtra(Constants.KEY_PKG_NAME, context.getPackageName());
    intent.putExtra(Constants.KEY_CLASS_NAME, launcherclassname);
    intent.putExtra("notificationNum", unReadCount);
    context.sendBroadcast(intent);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • OPPO

    try {
    Bundle extras = new Bundle();
    extras.putInt("app_badge_count", count);
    context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"), "setAppBadgeCount", String.valueOf(count), extras);
    } catch (Throwable th) {}
    • 1
    • 2
    • 3
    • 4
    • 5

Method: getLauncherClassName

public static String getLauncherClassName(Context context) {
       if(context == null){
           return null;
       }    
        PackageManager pm = context.getPackageManager();
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");
        try {
            for (ResolveInfo resolveInfo : pm.queryIntentActivities(intent, 0)) {
                if (resolveInfo.activityInfo.applicationInfo.packageName.equalsIgnoreCase(context.getPackageName())) {
                    return resolveInfo.activityInfo.name;
                }
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

除了上述的代码,还需要在清单文件声明对应的权限

<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"/>
<uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE"/>
  • 1
  • 2
  • 3
  • 4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值