Icon with notification counter

1

Does anyone know how I do a notifications counter on Android like Facebook, whatsapp, follow the photo showing the phone icon. ! link

    
asked by anonymous 09.07.2014 / 16:32

2 answers

2

Pure Android does not have this feature, manufacturers add this functionality to their UI and is different in each of them to use this feature.

In the Samsung Touchwiz case it would look like this:

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;
}

This one I already tested and I know it works on a Samsung Galaxy Tab 2, you can check it in the StackOverflow topic from where I took the code, there is also an example for devices from Sony, but I never got to test.

    
11.07.2014 / 21:21
1

There is this library that does exactly this here . Android does not allow you to change the icon after the APK is generated, basically the View Badger library create a widget above the application icon.

Remember that the creation of counters above the icon is considered as bad practice, according to the documentation design of Android. Notifications are displayed in the notification bar.

    
11.07.2014 / 20:35