如何在我的android应用程序中的启动器图标上显示徽章计数?
问题描述:
我曾尝试下面的意图方法,但它不能正常工作,如何在我的android应用程序中的启动器图标上显示徽章计数?
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", 3);
intent.putExtra("badge_count_package_name", getApplicationContext().getPackageName());
intent.putExtra("badge_count_class_name", MainActivity.class.getName());
getApplicationContext().sendBroadcast(intent);
答
有做的不标准的方式。但是你可以参考第三方库,但它只支持很少的设备。 您可以检查以下库: https://github.com/leolin310148/ShortcutBadger
+0
这个库不会在nexus设备上显示獾(由LG制造) – blackHawk
答
它可以在三星的TouchWiz发射
public static void setBadge(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);
}
public static String getLauncherClassName(Context context) {
PackageManager pm = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfos) {
String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
if (pkgName.equalsIgnoreCase(context.getPackageName())) {
String className = resolveInfo.activityInfo.name;
return className;
}
}
return null;
}
OR
参考链接: -
1>https://github.com/leolin310148/ShortcutBadger
2 >How to display count of notifications in app launcher icon
3>How to show notification count on app icon like Facebook?
4>Show Badge count on App icon in Android
5>实施例: - https://github.com/leolin310148/ShortcutBadger
希望这是帮助你上的应用程序图标显示徽章计数。
使用这个很酷的[库](https://github.com/leolin310148/ShortcutBadger) –