Android应用图标上的小红点Badge实现

为了增加APP的日活,在Android中有许多的应用仿苹果的在应用图标上显示小红点。当然有着一些手机ROM对小红点的支持,比如小米,三星等。

我们下面看下如何实现:

MainActivity:

public class MainActivity extends AppCompatActivity {private Handler handler = new Handler();private Runnable runnable = new MyRunnable();public class MyRunnable implements Runnable {@Overridepublic void run() {handler.postDelayed(runnable, 5000);BadgeUtil.setBadgeCount(getApplicationContext(), getCount(), R.mipmap.ic_launcher);}}private int getCount() {return new Random().nextInt(100);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button start = (Button) findViewById(R.id.start);Button stop = (Button) findViewById(R.id.stop);start.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (handler == null) {handler = new Handler();}if (runnable == null) {runnable = new MyRunnable();}handler.post(runnable);}});stop.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (handler != null) {BadgeUtil.resetBadgeCount(getApplicationContext(), R.mipmap.ic_launcher);handler.removeCallbacksAndMessages(null);runnable = null;handler = null;}}});}
}

BadgeUtil:目前支持小米,三星,LG,索尼,HTC等手机

public final class BadgeUtil {private BadgeUtil() throws InstantiationException {throw new InstantiationException("This class is not for instantiation");}/*** 设置Badge 目前支持Launcher*/public static void setBadgeCount(Context context, int count, int iconResId) {if (count <= 0) {count = 0;} else {count = Math.max(0, Math.min(count, 99));}if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) {setBadgeOfMIUI(context, count, iconResId);} else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {setBadgeOfSony(context, count);} else if (Build.MANUFACTURER.toLowerCase().contains("samsung") ||Build.MANUFACTURER.toLowerCase().contains("lg")) {setBadgeOfSumsung(context, count);} else if (Build.MANUFACTURER.toLowerCase().contains("htc")) {setBadgeOfHTC(context, count);} else if (Build.MANUFACTURER.toLowerCase().contains("nova")) {setBadgeOfNova(context, count);} else {Toast.makeText(context, "Not Found Support Launcher", Toast.LENGTH_LONG).show();}}/*** 设置MIUI的Badge*/private static void setBadgeOfMIUI(Context context, int count, int iconResId) {NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);NotificationCompat.Builder builder = new NotificationCompat.Builder(context);builder.setContentTitle("标题").setContentText("消息正文").setSmallIcon(iconResId);Notification notification = builder.build();try {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();}mNotificationManager.notify(0, notification);}/*** 设置索尼的Badge* 需添加权限:<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />*/private static void setBadgeOfSony(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);}/*** 设置三星的Badge\设置LG的Badge*/private static void setBadgeOfSumsung(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 setBadgeOfHTC(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 setBadgeOfNova(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);}public static void setBadgeOfMadMode(Context context, int count, String packageName, String className) {Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");intent.putExtra("badge_count", count);intent.putExtra("badge_count_package_name", packageName);intent.putExtra("badge_count_class_name", className);context.sendBroadcast(intent);}/*** 重置Badge*/public static void resetBadgeCount(Context context, int iconResId) {setBadgeCount(context, 0, iconResId);}public static String getLauncherClassName(Context context) {PackageManager packageManager = context.getPackageManager();Intent intent = new Intent(Intent.ACTION_MAIN);intent.setPackage(context.getPackageName());intent.addCategory(Intent.CATEGORY_LAUNCHER);ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);if (info == null) {info = packageManager.resolveActivity(intent, 0);}return info.activityInfo.name;}
}


参考文章:Badge分析&如何逼死处女座

Android应用图标上的小红点Badge实现相关推荐

  1. android badge,Android应用图标上的小红点Badge实现

    原文:http://blog.csdn.net/qq_33689414/article/details/53914439 Android应用图标上的小红点Badge实现 为了增加APP的日活,在And ...

  2. android 小红点自动显示,Android应用图标上的小红点Badge实践代码

    现在Android中有许多的应用仿苹果的在应用图标上显示小红点.当然有着一些手机ROM对小红点的支持,比如小米,三星等.google的api是没有提供这项工能的,这项功能一般都是厂商自己定制的,所以让 ...

  3. macos关闭软件更新小红点_如何一键消除手机上的小红点?请看这招...

    点击上方的 APP猿找到右上角的 ··· 设置为 ☆星标置顶,就可以随时找到猿仔!最近啊,猿仔身边的朋友发微信来问:怎么把iPhone上的微信.信息.邮件呀等图标的小红点给快速取消掉,每天看着总觉得莫 ...

  4. 实现Android应用图标的上的数字角标

    在做一些推送服务的时候,想在应用图标上添加未读消息,Android桌面角标的适配确实是非常坑爹的需求.原生系统根本就没有这个功能,国内很多厂家效仿ios都自己定义了该功能. 现在就为大家介绍部分机型添 ...

  5. Xamarin Android 应用程序内图标上数字提示

    最近在用 Xamarin 做一个 Android 应用,打开应用时,如果有新消息,需要在应用内的 Toolbar 或者首页的图标上显示数字提示.在这里和大家分享一下实现方法,如果你有更新好的实现方法, ...

  6. 对于ANDROID 5.0及其以上版本WIFI图标上显示感叹号的原因分析及解决方法

    这里我指的是WIFI可正常连接网络使用的前提下,手机状态栏WIF图标上仍出现感叹号的情况. 原因: 从 Android 5.0 开始,当安卓系统连接网络时,系统会向Google的某个特定的网址(htt ...

  7. [转]关于 android 5.0 网络图标上的感叹号及其解决办法

    转自:https://xn--yet824cpd.xn--fiqs8s/45.html 升级了安卓5.0的同学们一定对网络图标上面的那个感叹号感到十分郁闷.安卓5.0引入了一种新的网络评估机制来评估网 ...

  8. Android图标上显示数字

    Android图标上显示数字或加上数字的功能,直到Android4.4也没有实现.所以要修改系统的应用launcher来实现该效果. 之前你可能要参考其他一些博客或资料,对launcher的架构有一个 ...

  9. android 应用图标 角标 显示未读消息

    Android桌面角标的适配确实是非常坑爹的需求.原生系统根本就没有这个功能,国内很多厂家效仿ios都自己定义了该功能.Android程序员就很苦逼,要适配很多机型.建议万不得已情况下还是不要进行这项 ...

最新文章

  1. 深度学习 Optimizer 梯度下降优化算法总结
  2. 最近读的那些性能测试书
  3. 1.0jpa 2.0_EasyCriteria 2.0 – JPA标准应该很容易
  4. 商汤科技-数据运维工程师-提前批笔试题目汇总
  5. python字母变成数字怎么办_在Python中将字母转换为数字
  6. dhcp 授权的原理
  7. JQuery判断元素是否存在
  8. arcgis for android 无法加载本地jpg影像解决办法
  9. [InnoDB系列] - InnoDB释放表空间
  10. firefly 环境配置所需工具
  11. Oracle数据库有哪几种启动方式
  12. 【手写数字识别】基于matlab Fisher分类手写数字识别 【含Matlab源码 505期】
  13. 怎样用python控制别人的电脑_python实现远程控制电脑
  14. C程序设计,贪吃蛇程序
  15. matlab中如何去掉多行注释_matlab注释多行的方法
  16. 使用JS代码将steam喜加一的免费游戏删除。
  17. Netapp存储 硬盘显示bad label的解决办法
  18. 1月份中国综合PMI指数为53.2% 企业生产经营活动总体增速加快
  19. 想转行学IT,Java怎么样?
  20. Kubernetes基础:Pod中的Pause容器

热门文章

  1. php之连接mssql(sql server)新手教程
  2. 【资损】资损防控的系统规范-收单类服务设计
  3. 1004: 惠民工程 (2013年中南大学研究生复试机试 )
  4. MySQL 自定义stuff函数
  5. 风力发电机 有功功率 无功功率 理论有功功率
  6. ECC原理和RocketChip Cache ECC实现
  7. css修改谷歌浏览器和火狐浏览器的滚动条样式
  8. 如何用好 Google 搜索引擎?
  9. 【那些年我们一起看过的论文】之《Real-Time Loop Closure in 2D LIDAR SLAM》
  10. ResidualCoder