0

我曾嘗試下面的意圖方法,但它不能正常工作,如何在我的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); 
+0

使用這個很酷的[庫](https://github.com/leolin310148/ShortcutBadger) –

回答

0

它可以在三星的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

希望這是幫助你上的應用程序圖標顯示徽章計數。

+0

它不工作其他(比三星)設備,你有沒有其他的想法呢? – Dhayalu

+0

是的,我在我的舊應用程序中實現。讓我找你,但請給點時間。 – InsaneCat

+0

使用這個庫100%可行: - https://github.com/leolin310148/SortcutBadger 如果它對你有用,那麼請做出正確的答案。希望它可以幫助你。 – InsaneCat

相關問題