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

* */
public class BadgeUtil {
/**
* Set badge count

* 针对 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("Xiaomi")) {sendToXiaoMi(notification, context, count);} else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {sendToSony(context, count);} else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {sendToSamsumg(context, count);} else if (Build.MANUFACTURER.toLowerCase().contains("htc")) {sendToHTC(context, count);}  else if (Build.MANUFACTURER.toLowerCase().contains("nova")) {sendToNova(context, count);}else if (Build.MANUFACTURER.toLowerCase().contains("huawei")) {String versionString = android.os.Build.VERSION.RELEASE;int version=Integer.parseInt(versionString.substring(0, 1));if(version>=5){sendToHuaWei(context, count);}}else if (Build.MANUFACTURER.toLowerCase().contains("zuk")) {sendToZUK(context, count);} else {

// Toast.makeText(context, “Not Support”, Toast.LENGTH_LONG).show();
}
}

/*** 向小米手机发送未读消息数广播* @param count*/
private static void sendToXiaoMi(Notification notification,Context context, int count) {try {Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");Object miuiNotification = miuiNotificationClass.newInstance();Field field = miuiNotification.getClass().getDeclaredField("messageCount");field.setAccessible(true);field.set(miuiNotification, String.valueOf(count == 0 ? "" : count));  // 设置信息数-->这种发送必须是miui 6才行

// Field field = notification.getClass().getDeclaredField(“extraNotification”);
//
// Object extraNotification = field.get(notification);
//
// Method method = extraNotification.getClass().getDeclaredMethod(“setMessageCount”, int.class);
//
// method.invoke(extraNotification, count);

    } catch (Exception e) {e.printStackTrace();// miui 6之前的版本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(count == 0 ? "" : count));context.sendBroadcast(localIntent);}
}/*** 向索尼手机发送未读消息数广播<br/>* 据说:需添加权限:<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /> [未验证]* @param count*/
private static void sendToSony(Context context, int count){String launcherClassName = getLauncherClassName(context);if (launcherClassName == null) {return;}boolean isShow = true;if (count == 0) {isShow = false;}Intent localIntent = new Intent();localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE",isShow);//是否显示localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",launcherClassName );//启动页localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));//数字localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名context.sendBroadcast(localIntent);
}

//华为手机
private static void sendToHuaWei(Context context, int count){
Bundle localBundle = new Bundle();
localBundle.putString(“package”, context.getPackageName());
localBundle.putString(“class”, getLauncherClassName(context));
localBundle.putInt(“badgenumber”, count);
context.getContentResolver().call(Uri.parse(“content://com.huawei.android.launcher.settings/badge/”), “change_badge”, null, localBundle);
}

/*** 向三星手机发送未读消息数广播* @param count*/
private static void sendToSamsumg(Context context, int count){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);
}
//HTC Badge
private static void sendToHTC(Context context, int count){Intent intentNotification = new Intent("com.htc.launcher.action.SET_NOTIFICATION");ComponentName localComponentName = new ComponentName(context.getPackageName(),getLauncherClassName(context));intentNotification.putExtra("com.htc.launcher.extra.COMPONENT", localComponentName.flattenToShortString());intentNotification.putExtra("com.htc.launcher.extra.COUNT", count);context.sendBroadcast(intentNotification);Intent intentShortcut = new Intent("com.htc.launcher.action.UPDATE_SHORTCUT");intentShortcut.putExtra("packagename", context.getPackageName());intentShortcut.putExtra("count", count);context.sendBroadcast(intentShortcut);
}
//Nova Badge
private static void sendToNova(Context context, int count){ContentValues contentValues = new ContentValues();contentValues.put("tag", context.getPackageName() + "/" +getLauncherClassName(context));contentValues.put("count", count);context.getContentResolver().insert(Uri.parse("content://com.teslacoilsw.notifier/unread_count"),contentValues);
}
//ZUK
private static void sendToZUK(Context context, int counts){       Bundle extra = new Bundle();ArrayList<String> ids = new ArrayList<String>();// 以列表形式传递快捷方式id,可以添加多个快捷方式idids.add("custom_id_1");ids.add("custom_id_2");extra.putStringArrayList("app_shortcut_custom_id", ids);extra.putInt("app_badge_count", counts);Bundle b = null;b = context.getContentResolver().call(Uri.parse("content://" + "com.android.badge" + "/" + "badge"),"setAppBadgeCount", null, extra);boolean result = false;if (b != null) {result = true;}else {result = false;  }  return;}
/*** 重置、清除Badge未读显示数<br/>* @param context*/
public static void resetBadgeCount(Notification notification,Context context) {setBadgeCount(notification, context, 0);
}/*** Retrieve launcher activity name of the application from the context** @param context The context of the application package.* @return launcher activity name of this application. From the*         "android:name" attribute.*/
private static String getLauncherClassName(Context context) {PackageManager packageManager = context.getPackageManager();Intent intent = new Intent(Intent.ACTION_MAIN);// To limit the components this Intent will resolve to, by setting an// explicit package name.intent.setPackage(context.getPackageName());intent.addCategory(Intent.CATEGORY_LAUNCHER);// All Application must have 1 Activity at least.// Launcher activity must be found!ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);// get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER// if there is no Activity which has filtered by CATEGORY_DEFAULTif (info == null) {info = packageManager.resolveActivity(intent, 0);}return info.activityInfo.name;
}

}

应用启动图标未读消息数显示 工具类相关推荐

  1. Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒

    在Android手机上,如QQ.微信当有未读消息的时候.我们可以看到在应用的启动图标的右上角会有一个红色圈圈.且圈圈里会动态显示未读消息的数目,如下图显示: 那么该功能是怎么实现的呢? 在万能的互联网 ...

  2. android底部导航栏带消息数的框架,GitHub - BarkSheep/Android-NavMenuLayout: 一个底部导航栏, 实现了显示未读消息数, 显示红点等效果的封装...

    Android-NavMenu-master 一个底部导航栏, 实现了显示未读消息数, 显示红点等效果的封装. 添加依赖 1. 在项目根目录的 build.gradle 中添加 allprojects ...

  3. android+仿qq未读消息数量,仿qq自定义未读消息数显示角标

    66FF020E13B921CB19C7542F4801AF43.png 如图所示,我们需要实现的效果 在消息tab上,是一个组合的自定义view,具体实现如下 android:layout_widt ...

  4. Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51082574 文章出自:薛瑄的博客 你也可以查看我的其他同类文章,也会让你有一定的 ...

  5. Android app图标显示未读消息数

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51082574 文章出自:薛瑄的博客 你也可以查看我的其他同类文章,也会让你有一定的 ...

  6. android 未读信息反复提醒,Android仿微信未读消息数提示显示数字BadgeView大于99条显示99+...

    [实例简介] Android仿微信未读消息数提示显示数字BadgeView大于99条显示99+ [实例截图] [核心代码] BadgeView └── BadgeView ├── app │   ├─ ...

  7. android仿微信实现消未读消息数在tab栏提醒

    最近公司有个项目,要仿照微信的未读消息数提醒,提醒用户通知公告的未读数,找了几个都不太好用,后来找前辈咨询,给了一个库,感觉特别实用,在这里分享给大家. 首先给大家看项目的原型图截图: 然后是代码实现 ...

  8. android图标未读消息,Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示...

    原理 添加角标的原理就是发送一个Broadcast(广播),在广播的Intent中指定需要被添加角标的应用的packageName(包名),className(类名),count(角标数目).当然了, ...

  9. Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示

    http://www.51itong.net/android-badgenumber-9789.html

最新文章

  1. 如何让网页不受电信114劫持
  2. 软件开发环境-集成机制
  3. 机器视觉学习笔记:BP神经网络详解
  4. JAVA线程本地变量ThreadLocal和私有变量的区别
  5. 【拔刀吧少年】之sed编辑器
  6. Delphi 计算儒略日(Julian day)的代码
  7. 计算机二级科目电子商务,计算机二级Web数据在电子商务中的应用解析
  8. 面试官系统精讲Java源码及大厂真题 - 28 Future、ExecutorService 源码解析
  9. 如何使用pywinauto实现一个股票自动交易系统?
  10. delphi(注入)附部分源代码
  11. 杨焘鸣:潜意识的特性
  12. 【近五千字纯手撸】✨前后一个月面试30家中大型高级、资深java工程师终获500强公司offer心得以及经过
  13. arm push/pop/b/bl汇编指令
  14. 谷歌翻译服务退出中国大陆,使用SwitchyOmega仍需要全文翻译,恢复访问的方法
  15. [读书笔记]结绳记事
  16. IGBT器件选型参考
  17. 基于opencv+python的角度测量
  18. 程序猿最讨厌康熙的哪个儿子?
  19. mysql字符集设置方案_MySQL的字符集配置
  20. 当linux中的所有指令突然不能使用的时候

热门文章

  1. eos区块链 java客户端_在EOS区块链上使用EOSJS和scatter开发dApp
  2. MySQL语句和命令大全
  3. 不断奔跑,却忘了来时的路
  4. [prometheus]Step11-prometheus动态监控服务器端口并告警
  5. Redis遇到过的问题(Could not get a resource from the pool)
  6. 管理者的50堂课之创始人篇 读书笔记
  7. pad看linux源码,在 iPad和 iPhone的浏览器上查看网页源代码
  8. ipad显示连接不到商店服务器,iPad无法连接App Store 打不开怎么办
  9. ubuntu20安装gdb插件gef的爬坑记录
  10. XSSFWorkbook 设置单元格样式_CVA高校精英计划第二弹:执行最佳操作,做好设置准备...