Friday, July 11, 2014

Android: iOS Style Badge App Icon for Sony, Samsung and HTC devices

This post is continue to one of my old post Android Get iOS Style Badge App Icon

Know a days we are seeing facebook,whatsapp and lots of apps are using badges on the app icon and  they are not replacing any app icon and they are able to achieve badge on app icon and we should be having question, How it is possible?

iOS style badge icon is achieved in android by few manufactures (Sony, Samsung, HTC...) but the way of implementation is different from each one of the manufacture for the badge on app icon.

Before implementing the badge on app icon, check the manufacture and then support.

// The manufacturer of the product/hardware.
Build.MANUFACTURER;

Lets go with each of the manufacture implementation:
Sony:

Manifest File changes:

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

Code Changes:

 try {  
      intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");  
      intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.vardhan.notificationbadgesample.MainActivity");  
      intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);  
      intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", badgeno);  
      intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", "com.vardhan.notificationbadgesample");  
      sendBroadcast(intent);  
 } catch (Exception localException) {  
      Log.e("CHECK", "Sony : " + localException.getLocalizedMessage());  
 }  


HTC:

Manifest File changes:

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


Code Changes:

 try {  
      Intent localIntent1 = new Intent("com.htc.launcher.action.UPDATE_SHORTCUT");  
      localIntent1.putExtra("packagename", "com.vardhan.notificationbadgesample");  
      localIntent1.putExtra("count", badgeno);  
      sendBroadcast(localIntent1);  
      Intent localIntent2 = new Intent("com.htc.launcher.action.SET_NOTIFICATION");  
      ComponentName localComponentName = new ComponentName(this, "com.vardhan.notificationbadgesample.MainActivity");  
      localIntent2.putExtra("com.htc.launcher.extra.COMPONENT",      localComponentName.flattenToShortString());  
         localIntent2.putExtra("com.htc.launcher.extra.COUNT", 10);  
      sendBroadcast(localIntent2);  
 } catch (Exception localException) {  
      Log.e("CHECK", "HTC : " + localException.getLocalizedMessage());  
 }  


Samsung:

Manifest File changes:

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


Code Changes:

 try {  
    ContentResolver localContentResolver = getContentResolver();  
    Uri localUri = Uri.parse("content://com.sec.badge/apps");  
    ContentValues localContentValues = new ContentValues();  
    localContentValues.put("package", "com.vardhan.notificationbadgesample");  
    localContentValues.put("class", "com.vardhan.notificationbadgesample.MainActivity");  
    localContentValues.put("badgecount", Integer.valueOf(badgeno));  
    String str = "package=? AND class=?";  
    String[] arrayOfString = new String[2];  
    arrayOfString[0] = "com.vardhan.notificationbadgesample";  
    arrayOfString[1] = "com.vardhan.notificationbadgesample.MainActivity";  
      int update = localContentResolver.update(localUri, localContentValues, str, arrayOfString);  
      if (update == 0) {  
           localContentResolver.insert(localUri, localContentValues);  
      }  
 } catch (IllegalArgumentException localIllegalArgumentException) {  
      Log.e("CHECK", "Samsung1F : " + localIllegalArgumentException.getLocalizedMessage());  
 } catch (Exception localException) {  
      Log.e("CHECK", "Samsung : " + localException.getLocalizedMessage());  
 }  


Output


Source Code
You can download the source code by clicking Source Code Click Here: .  This project is built using eclipse IDE. Unzip and import the project into Eclipse, it’s a good idea to use the Project by clean and rebuild from the project menu. It works in all API levels above 9

Thanks for reading :) 
Whether this post is helpful?

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? comments/feedback are welcome.

9 comments :

  1. This has been helpful- but one thing this post doesn't cover is how to get the previous badge count so that it can be incremented (Example uses random number).

    I figured out how to do it for Samsung:

    //determine existing badge count
    Cursor cursor = localContentResolver.query(localUri,null,null,null,null);
    if(cursor != null)
    {
    cursor.moveToPosition(-1);
    while (cursor.moveToNext())
    {
    if(packageName.equals(cursor.getString(1)) && classPath.equals(cursor.getString(2)))
    {

    badgeCount = cursor.getInt(3);
    break;
    }
    }
    }
    //increment badge Count
    ++badgeCount;

    But how do you do it for htc and sony? Do you have to keep track of the badge count separately by using shared preferences perhaps?

    ReplyDelete
    Replies
    1. Thanks for the comment.
      My aim is to get badge on app icon.
      If we look into iOS, the badge count is from back end server not from the mobile side and the no of notifications. Now for android, we can track how many number of notification are existing and depend on them we can get the count and update the same on app icon.

      Delete
  2. How can i change badge color in android

    ReplyDelete
  3. thanks for this tut, but how about LG ? :(

    ReplyDelete
  4. how to do for digiflip device plz help me

    ReplyDelete
  5. Failed to find provider info for com.sec.badge
    Unknown URI content://com.sec.badge/apps

    Found this error, help me!!!

    ReplyDelete
  6. Failed to find provider info for com.sec.badge
    Unknown URI content://com.sec.badge/apps

    this two error show up after using example of samsung..

    ReplyDelete
  7. How to make it work in Xiaomi MIUI devices????Please tell me

    ReplyDelete