5

you may think that this is a duplicate question but i want answers to be more specific than any questions that are similar to this.

first it says that like facebook having notification badge with samsung device. How does Facebook add badge numbers on app icon in Android?. and it turns out to be the touchwiz launcher which may be the default launcher of samsung devices. when i tried to test my samsung device with nova launcher. it needs to install some third party software or Nova TestlaUnread.

Heres the fact. i thought the touchWiz is adding badge for all application, but when i try to test with my app to have some notification, it doesn't show any badge. as i found out that samsung TouchWiz only showing app to selected app as stated here: UI help for Notification on icon

The question is: is there any way to let know the touchWiz to add badge to my app-icon? That's for the samsung device,

with that previous arguments, it seems like the launcher of different vendors are the one responsible for the badges in app icon in android. Sony Experia uses Experia Home/Launcher as its default launcher. in my sony device, they had some badge in there sms or miss calls or facebook.

The question is: like the previous question, is there any way to let know experia home to add badge to my app icon?

because i was thinking maybe interacting with the launcher seems the solution of this "adding badge to app icon".

and there's this solution about this activity alias and changing the app icon from time to time which i think is rubbish. Is there a way to add a badge to an application icon in Android?

so i'm seeking solution on interacting with the launchers which is vendor specific.

'any solution will be appreciated even i would go deep with using native development in android'

Thanks ahead

Community
  • 1
  • 1
Vik2r
  • 135
  • 1
  • 2
  • 7
  • Yes I have written up a how-to here http://stackoverflow.com/questions/20136483/how-do-you-interface-with-badgeprovider-on-samsung-phones/20136484#20136484 – Daniel Ochoa Nov 22 '13 at 03:40
  • Possible duplicate of [Is there a way to add a badge to an application icon in Android?](https://stackoverflow.com/questions/2905542/is-there-a-way-to-add-a-badge-to-an-application-icon-in-android) – Shirish Herwade Nov 14 '17 at 11:09

2 Answers2

4

I use this class for Samsung and Sony devices (also available https://gist.github.com/Tadas44/cdae2f5995f21bf1c27f). Don't forget to add <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /> to AndroidManifest.xml

public class BadgeUtils {


    public static void setBadge(Context context, int count) {
        setBadgeSamsung(context, count);
        setBadgeSony(context, count);
    }

    public static void clearBadge(Context context) {
        setBadgeSamsung(context, 0);
        clearBadgeSony(context);
    }


    private static void setBadgeSamsung(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 setBadgeSony(Context context, int count) {
        String launcherClassName = getLauncherClassName(context);
        if (launcherClassName == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());

        context.sendBroadcast(intent);
    }


    private static void clearBadgeSony(Context context) {
        String launcherClassName = getLauncherClassName(context);
        if (launcherClassName == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(0));
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());

        context.sendBroadcast(intent);
    }

    private 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;
    }
}
Tadas Valaitis
  • 870
  • 10
  • 11
  • 1
    It worked well for Samsung devices in my case but it seems now it's only for Samsung & Sony devices. So, we need to update this answer to make it working for other manufacturers too. – Harpreet Sep 03 '15 at 10:35
0

In addition to Daniel Ochoa's solution for Samsung (see comment on OP), I have figured out how this is done for Sony devices.

I've blogged about it here. I've also posted a seperate SO question about this here.


Sony devices use a class named BadgeReciever.

  1. Declare the com.sonyericsson.home.permission.BROADCAST_BADGE permission in your manifest file:

  2. Broadcast an Intent to the BadgeReceiver:

    Intent intent = new Intent();
    
    intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.yourdomain.yourapp.MainActivity");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "99");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", "com.yourdomain.yourapp");
    
    sendBroadcast(intent);
    
  3. Done. Once this Intent is broadcast the launcher should show a badge on your application icon.

  4. To remove the badge again, simply send a new broadcast, this time with SHOW_MESSAGE set to false:

    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
    

I've excluded details on how I found this to keep the answer short, but it's all available in the blog. Might be an interesting read for someone.

Community
  • 1
  • 1
Marcus
  • 1,623
  • 1
  • 16
  • 22
  • Since this question has no answers and you have no rep, I'm not going to flag this (I've been given your answer to review). I suggest you pad the question with some relevant information from those links though. I've up voted your other question so you can answer it. – OGHaza Nov 26 '13 at 15:24
  • Thanks @OGHaza. I have added some code to this answer and will answer my main question about it now. – Marcus Nov 26 '13 at 15:28