好久没写博客了,这段时间一直在做项目忙得要死啊。然后新需求来了,领导要求做一个像iOS那样能提醒用户未读消息数量的角标。查了下资料出现频率最多的就是ShortcutBadger和ShortcutHelper,这两个项目兼容了挺多手机,但是一看这两个项目的更新时间都有点久了,使用之后也发现有好多都已经不起作用了。

官方提供

华为角标官方文档:

华为还是蛮不错的提供了角标的详细使用,但不要忘了添加权限。

注:关于华为文档链接可能失效的问题,到华为开发者页面搜索角标,能找到华为角标开发指南书。

小米角标官方文档:

小米做的也挺厚道,在开放平台上搜索就能找到角标的使用,但仍存在两个问题。

联想ZUK官方文档:

虽然在市面上占的手机份额不是很多,但却也给出了详细的使用角标的方法。

索尼官方文档:

索尼官方给出的有点麻烦,官网搜索badge之后看了好几个答案才发现了官方使用方法。

谷歌官方文档:

这是Android 8.0新出现的,但也仅仅是圆点,没有数字颜色也不能控制。

具体机型兼容

华为

华为对外提供了数据库URI、操作数据库的权限,通过指定的接口方式,APP将需要显示的角标数量和应用的一些包名之类的信息传给华为桌面便能显示未读角标信息了,但不要忘了添加权限。

    private static void badgeHuawei(Context context, int badgeCount) {try {Bundle bunlde = new Bundle();bunlde.putString("package", context.getPackageName());bunlde.putString("class", launcherClassName);bunlde.putInt("badgenumber", badgeCount);context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bunlde);} catch (Exception e) {Log.e(TAG, e.getMessage());}}
    <uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE " />

小米

小米虽然给出了官方文档但还是存在两个问题:

1、小米手机在添加角标方法执行的时候如果在APP内或未清理掉之前通知,角标不会出现;

    private static void badgeXiaomi(final Context context, final int badgeCount) {new Handler().postDelayed(new Runnable() {@Overridepublic void run() {//延迟1秒是为了避免执行操作的时候还在app内,如要真正避免还是需要控制调用的时机try {Notification notification = getNotification(context, badgeCount);Field field = notification.getClass().getDeclaredField("extraNotification");Object extraNotification = field.get(notification);Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);method.invoke(extraNotification, badgeCount);BadgerUtil.notify(notification, badgeCount);} catch (Exception e) {e.printStackTrace();// 网上找的据说是miui 6之前的版本,没有miui6之前版本的小米手机不知道有没有效Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");localIntent.putExtra("android.intent.extra.update_application_component_name", context.getPackageName() + "/" + getLauncherClassName(context));localIntent.putExtra("android.intent.extra.update_application_message_text", String.valueOf(badgeCount == 0 ? "" : badgeCount));context.sendBroadcast(localIntent);}}}, 1000);}

2、Android 8.0对通知的使用做了调整需要使用先设置NotificationChannel,否则会提示错误信息,具体可以看官方文档;

    private static NotificationManager notificationManager;private static Notification getNotification(Context context, int badgeCount) {if (notificationManager == null) {notificationManager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0之后添加角标需要NotificationChannelNotificationChannel channel = new NotificationChannel("badge", "badge", NotificationManager.IMPORTANCE_LOW);channel.setShowBadge(true);((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);}}Notification notification = new NotificationCompat.Builder(context, "badge").setContentTitle("通知").setContentText("新消息").setSmallIcon(R.drawable.ic_launcher).setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL).setNumber(badgeCount).setAutoCancel(true).build();return notification;}/*** 发送通知*/private static void notify(Notification notification, int badgeCount) {notificationManager.cancel(TAG, NOTIFY_ID);if (badgeCount > 0) {notificationManager.notify(TAG, NOTIFY_ID, notification);}}

联想ZUK

zuk的使用和联想差不多也是提供操作数据库的,同时也需要添加权限。

    private static void badgeZuk(Context context, int badgeCount) {Bundle extra = new Bundle();extra.putInt("app_badge_count", badgeCount);context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"), "setAppBadgeCount", null, extra);}
    <uses-permission android:name="android.permission.READ_APP_BADGE" />

索尼

索尼官方给出的方法和网上大部分在使用的方法好像不一样,由于没有索尼手机没法测试,权限也有点不一样,我这里全加上了。

    private static void badgeSony(Context context, int badgeCount) {if (asyncQueryHandler == null) {asyncQueryHandler = new AsyncQueryHandler(context.getContentResolver()) {};}try {//官方给出方法ContentValues contentValues = new ContentValues();contentValues.put("badge_count", badgeCount);contentValues.put("package_name", context.getPackageName());contentValues.put("activity_name", launcherClassName);asyncQueryHandler.startInsert(0, null, Uri.parse("content://com.sonymobile.home.resourceprovider/badge"), contentValues);} catch (Exception e) {//网上大部分使用方法Intent intent = new Intent("com.sonyericsson.home.action.UPDATE_BADGE");intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", badgeCount > 0);intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(badgeCount > 0 ? badgeCount : ""));intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());context.sendBroadcast(intent);}}
    <uses-permission android:name="com.sonymobile.home.permission.PROVIDER_INSERT_BADGE" /><uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /><uses-permission android:name="com.sonyericsson.home.action.UPDATE_BADGE" />

三星

在ShortcutBadger中使用了数据库操作的方式但又添加了废弃的注释,或许老版本有使用,新版本不能使用了吧。

    private static void badgeSamsung(Context context, int badgeCount) {Uri uri = Uri.parse("content://com.sec.badge/apps");String columnId = "_id";String columnPackage = "package";String columnClass = "class";String columnBadgeCount = "badgeCount";Cursor cursor = null;try {ContentResolver contentResolver = context.getContentResolver();cursor = contentResolver.query(uri, new String[]{columnId}, columnPackage + "=?", new String[]{context.getPackageName()}, null);if (cursor == null || !cursor.moveToFirst()) {ContentValues contentValues = new ContentValues();contentValues.put(columnPackage, context.getPackageName());contentValues.put(columnClass, launcherClassName);contentValues.put(columnBadgeCount, badgeCount);contentResolver.insert(uri, contentValues);} else {int idColumnIndex = cursor.getColumnIndex(columnId);ContentValues contentValues = new ContentValues();contentValues.put(columnBadgeCount, badgeCount);contentResolver.update(uri, contentValues, columnId + "=?", new String[]{String.valueOf(cursor.getInt(idColumnIndex))});}} catch (Exception e) {Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");intent.putExtra("badge_count", badgeCount);intent.putExtra("badge_count_package_name", context.getPackageName());intent.putExtra("badge_count_class_name", launcherClassName);context.sendBroadcast(intent);} finally {if (cursor != null) {cursor.close();}}}
    <uses-permission android:name="com.sec.android.provider.badge.permission.READ" /><uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />

HTC

经测试,HTC只需要发送广播即可,也不需要权限,5.0,7.0亲测有效

    private static void badgeHtc(Context context, int badgeCount) {try {Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");intent.putExtra("badge_count", badgeCount);intent.putExtra("badge_count_package_name", context.getPackageName());intent.putExtra("badge_count_class_name", launcherClassName);context.sendBroadcast(intent);} catch (Exception e) {}}

谷歌

Android 8.0给出了一种没有数字且不能改变颜色的圆点角标

    private static NotificationManager notificationManager;private static Notification getNotification(Context context, int badgeCount) {if (notificationManager == null) {notificationManager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0之后添加角标需要NotificationChannelNotificationChannel channel = new NotificationChannel("badge", "badge", NotificationManager.IMPORTANCE_LOW);channel.setShowBadge(true);((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);}}Notification notification = new NotificationCompat.Builder(context, "badge").setContentTitle("通知").setContentText("新消息").setSmallIcon(R.drawable.ic_launcher).setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL).setNumber(badgeCount).setAutoCancel(true).build();return notification;}/*** 发送通知*/private static void notify(Notification notification, int badgeCount) {notificationManager.cancel(TAG, NOTIFY_ID);if (badgeCount > 0) {notificationManager.notify(TAG, NOTIFY_ID, notification);}}

OPPO和vivo

这两个手机由于广告和线下店的原因在市场上占的份额还是相当多的,但是对角标的使用却非常坑。首先是OPPO,没有官方文档,找客服之后,只能说非常非常非常麻烦

然后没办法咯,只能看看人家QQ和微信是怎么实现的了

QQ实现:

微信实现:

网上查了下也是这么实现的,添加到代码中使用,并没有什么用,一时好奇把包名改成com.tencent.mobileqq,恭喜您的角标上线了。。。OPPO和vivo都可以了。。。无语啊。。。

源码

详细代码可以看GitHub,如有问题也可以留言一起交流。

【笔记】Android桌面角标Badge官方文档和兼容性解决相关推荐

  1. Android桌面角标调研

    一.背景    安卓应用的角标是由Launcher支持的,而原生的Android系统Launcher并没有提供角标支持,所以各大手机厂商只能自己定制Launcher来实现,然后提供接口给外部使用. 二 ...

  2. 【Android 命令行工具】Android 命令行工具简介 ( 官方文档 | SDK 命令行工具 | SDK 构建工具 | SDK 平台工具 | 模拟器工具 | Jetifier 工具 )

    文章目录 一.官方文档 二.Android 命令行工具简介 1.SDK 命令行工具 2.SDK 构建工具 3.SDK 平台工具 4.模拟器工具 5.Jetifier 工具 一.官方文档 Android ...

  3. Android5更新包,Android 5.x OTA Update官方文档(四、OTA更新包解读)

    http://blog.csdn.net/huangyabin001/article/details/44873753 写在前面: 写在前面:若能直译,不会意译,意译不成,为了选择不误导他人也会漏译无 ...

  4. 安卓CTS官方文档之兼容性测试套件简介

    官方英文文档原文:https://source.android.com/compatibility/cts-intro.html Compatibility Test Suite 兼容性测试套件 Ho ...

  5. MySQL8.0.28安装教程全程参考MySQL官方文档

    MySQL8.0.28详细安装教程.提供了Windows10下安装MariaDB与MySQL8.0同时共存的方法,以及Linux发行版Redhat7系列安装MySQL8.0详细教程.Windows10 ...

  6. Android 应用桌面角标显示

    Android 应用桌面角标显示 前言 ### 什么是角标? ​ 首先如图1所示,角标就是当有消息来的时候,桌面上应用的图标会用数字显示新消息的条数. ​ 应用角标先出现在ios系统上,apple用于 ...

  7. Android 实现应用桌面角标

    1.小米手机 官方文档:文档中心 /*** 小米手机创建通知信息并创建角标** @param context* @param num*/public static void setXiaoMiBadg ...

  8. [贝聊科技]有关Android应用桌面角标(BadgeNumber)实现的探讨

    作者:小强 贝聊移动开发部 Android工程师 前言:本文主要讲述了以下三方面: 怎么在Android系统下让自家的应用图标像iOS系统那样支持数字角标的显示? 在网上找不到现成的解决方案的情况下, ...

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

    public static void setBadgeNum(Context context, int number) {//context对象,小红点的数量if (isEmui()) {//这个是华 ...

最新文章

  1. 系统架构设计_系统工程师--系统架构设计
  2. js模仿flash写字动画
  3. 使用bpf 排查 fd 泄漏
  4. es6学习笔记11--Proxy和Reflect
  5. python在汽车上的应用_python实现图片识别汽车功能
  6. 服务器可以读u盘文件,服务器向u盘拷贝数据库
  7. 策略模式-Golang实现
  8. [js] script所在的位置会影响首屏显示时间吗
  9. 如何快速完成企业应用IPv6改造
  10. php面试题之一——PHP核心技术(高级部分)
  11. opencv-api boundingRect
  12. 深度学习硬件加速综述写作心得
  13. 一周信创舆情观察(7.5~7.11)
  14. M个苹果放到N个相同盘子和N个不同盘子的解法
  15. 台式机外接显示器后没有声音
  16. Coursera TensorFlow 基础课程-week1
  17. 社区养老实时可查,移动GIS+WebGIS助老人安享晚年
  18. cocos2dx之加载瓦片地图tmx
  19. 企业数字化转型的核心是什么?如何才能真正做到数字化转型?
  20. C#:圣诞节内卷,铅笔画制作

热门文章

  1. 如何实现WiFi与5G无缝切换?如何进行无线通信切换测试?(二)
  2. 图像倾斜校正 Radon 变换原理及函数
  3. Android 实现微信界面
  4. Spring Boot 自定义注解支持EL表达式(基于 MethodBasedEvaluationContext 实现)
  5. oracle rfs进程过多,DATA GUARD的RFS进程的问题
  6. python中的序列封包和序列解包
  7. Java TCP实现高仿版QQ聊天(一)
  8. uniapp选中多张图片或者视频(多图上传)
  9. 2022 - 软件构造复习
  10. bootstrap 字体太大怎么办