这篇文章给大家分享的是有关Android应用图标上小红点Badge的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。效果图:直接上代码吧,代码中有注释:直接上代码吧,代码中有注释:BadgeUtil类:pu
这篇文章给大家分享的是有关Android应用图标上小红点Badge的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
效果图:
直接上代码吧,代码中有注释:
直接上代码吧,代码中有注释:
BadgeUtil类:
public class BadgeUtil { private BadgeUtil() throws InstantiationException { throw new InstantiationException("This class is not for instantiation"); } 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 if (Build.MANUFACTURER.toLowerCase().contains("OPPO")) {//oppo //setBadgeOfOPPO(context, count); }else if (Build.MANUFACTURER.toLowerCase().contains("LeMobile")) {//乐视 } else if (Build.MANUFACTURER.toLowerCase().contains("vivo")) { setBadgeOfVIVO(context, count); }else if (Build.MANUFACTURER.toLowerCase().contains("HUAWEI")||Build.BRAND.equals("Huawei")||Build.BRAND.equals("HONOR")) {//华为 setHuaweiBadge(context, count); }else if (Build.MANUFACTURER.toLowerCase().contains("")) {//魅族 }else if (Build.MANUFACTURER.toLowerCase().contains("")) {//金立 }else if (Build.MANUFACTURER.toLowerCase().contains("")) {//锤子 }else { //Toast.makeText(context, "Not Found Support Launcher", Toast.LENGTH_LONG).show(); } } 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); } 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); } 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); } 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); } 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); } private static void setBadgeOfVIVO(Context context,int count){ try { Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM"); intent.putExtra("packageName", context.getPackageName()); String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName(); intent.putExtra("className", launchClassName); intent.putExtra("notificationNum", count); context.sendBroadcast(intent); }catch (Exception e){ e.printStackTrace(); } } private static void setBadgeOfOPPO(Context context,int count){ 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 (Exception e) { e.printStackTrace(); } } public static void setHuaweiBadge(Context context, int count) { try { Bundle bundle = new Bundle(); bundle.putString("package", context.getPackageName()); String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName(); bundle.putString("class", launchClassName); bundle.putInt("badgenumber", count); context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bundle); } catch (Exception e) { e.printStackTrace(); } } 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); } 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; }}
感谢各位的阅读!关于“Android应用图标上小红点Badge的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
--结束END--
本文标题: Android应用图标上小红点Badge的示例分析
本文链接: https://lsjlt.com/news/221963.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0