How to interface with the BadgeProvider on Samsung phones to add a count to the app icon?

Samsung's TWLauncher allows apps to create badge counts on app icons.

This is completely undocumented! There is no mention of it anywhere, and only a handful of apps are using it (e.g. Facebook, eBay).

How do you use this functionality to add a count to your app icon?

This is very specific to Samsung devices. I am not asking about Android in general. I'm only asking about badging Samsung's Touchwhiz interface which currently allows badging. Android does not.


Solution 1:

First you'll need to add the following permissions to your AndroidManifest.xml file.

<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />

The column structure is as follows:

(integer) _id, (text) package, (text) class, (integer) badgecount, (blob) icon, (???) extraData

In order to query ALL results from the BadgeProvider do the following:

// This is the content uri for the BadgeProvider
Uri uri = Uri.parse("content://com.sec.badge/apps");

Cursor c = getContentResolver().query(uri, null, null, null, null);

// This indicates the provider doesn't exist and you probably aren't running
// on a Samsung phone running TWLauncher. This has to be outside of try/finally block
if (c == null) {
    return;
}

try {
    if (!c.moveToFirst()) {
        // No results. Nothing to query
        return;
    }

    c.moveToPosition(-1);
    while (c.moveToNext()) {
        String pkg = c.getString(1);
        String clazz = c.getString(2);
        int badgeCount = c.getInt(3);
        Log.d("BadgeTest", "package: " + pkg + ", class: " + clazz + ", count: " + String.valueOf(cnt));
    }
} finally {
    c.close();
}

In order to add a badge count to your application icon

ContentValues cv = new ContentValues();
cv.put("package", getPackageName());
// Name of your activity declared in the manifest as android.intent.action.MAIN.
// Must be fully qualified name as shown below
cv.put("class", "com.example.badge.activity.Test");
cv.put("badgecount", 1); // integer count you want to display

// Execute insert
getContentResolver().insert(Uri.parse("content://com.sec.badge/apps"), cv);

If you want to clear the badge count on your icon

ContentValues cv = new ContentValues();
cv.put("badgecount", 0);
getContentResolver().update(Uri.parse("content://com.sec.badge/apps"), cv, "package=?", new String[] {getPackageName()});  

NEW
I have created an open source project that you can import as a library to assist with this. It's licensed as Apache so feel free to use it as you please.

You can get it from here: https://github.com/shafty023/SamsungBadger

Solution 2:

There is another cool open source library that support different devices: https://github.com/leolin310148/ShortcutBadger/

Solution 3:

add these permissions to manifest

<!--for android badge-->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS"/>

<!--for Samsung badge-->
<uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>

<!--for htc badge-->
<uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT"/>

<!--for sony badge-->
<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"/>

<!--for apex badge-->
<uses-permission android:name="com.anddoes.launcher.permission.UPDATE_COUNT"/>

add these package names to your class :

    final String HOME_PACKAGE_SONY = "com.sonyericsson.home";
    final String HOME_PACKAGE_SAMSUNG = "com.sec.android.app.launcher";
    final String HOME_PACKAGE_LG = "com.lge.launcher2";
    final String HOME_PACKAGE_HTC = "com.htc.launcher";
    final String HOME_PACKAGE_ANDROID = "com.android.launcher";
    final String HOME_PACKAGE_APEX = "com.anddoes.launcher";
    final String HOME_PACKAGE_ADW = "org.adw.launcher";
    final String HOME_PACKAGE_ADW_EX = "org.adwfreak.launcher";
    final String HOME_PACKAGE_NOVA = "com.teslacoilsw.launcher";

for use :

  // just put your pachage and main activity class path
  String classPath = "ir.faasaa.resa.MainActivity";

           ContentValues cv = new ContentValues();
                    cv.put("package", context.getPackageName());
                    cv.put("class", classPath);
                    cv.put("badgecount", count);
           context.getContentResolver().insert(Uri.parse(HOME_PACKAGE_SAMSUNG), cv);

thanks to ShortcutBadger