如何清除android中的旧徽章数量

时间:2016-09-09 04:37:17

标签: android count badge

我设置" 0"表示其显示徽章计数为" 1"。如何清除我的旧徽章数。

//徽章计数集方法

  public static void setBadge(Context mContext, int count) {
    String launcherClassName = getLauncherClassName(mContext);
    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", mContext.getPackageName());
    intent.putExtra("badge_count_class_name", launcherClassName);
    mContext.sendBroadcast(intent);
}

//找出我们的应用程序,然后返回应用程序包名称。

 public static String getLauncherClassName(Context mContext) {

    PackageManager pm = mContext.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(mContext.getPackageName())) {
            String className = resolveInfo.activityInfo.name;
            return className;
        }
    }
    return null;
}

1 个答案:

答案 0 :(得分:0)

如果通过调用方法使用https://github.com/leolin310148/ShortcutBadger库,可以执行此操作:

ShortcutBadger.applyCount(Context context, int badgeCount)

//or

ShortcutBadger.removeCount(Context context)