Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Launcher icon notification for Samsung and Sony Xperia devices
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;
}
}
@alred
Copy link

alred commented Jun 13, 2015

Configurndo plis

@duggisetty
Copy link

duggisetty commented Dec 22, 2015

it is not worked for all mobiles like Motorola,Lenovo,Micormax..etc

@ishaileshmishra
Copy link

ishaileshmishra commented Jun 20, 2016

Where to use this code

@PrashantAryan
Copy link

PrashantAryan commented Jun 23, 2016

it is not worked for all mobiles like Motorola,Lenovo,Micormax

Copy link

ghost commented Nov 18, 2016

@duggisetty & @saurovo – The title clearly states “Launcher icon notification for Samsung and Sony Xperia devices (emphasis mine). It doesn’t mention anywhere that the code snippet supports any other devices besides that. Now guess why Motorola, Lenovo, Micormax, etc. don’t work… (that is, unless you fork this gist and implement support for those other devices yourself).

@Siddharth3
Copy link

Siddharth3 commented Feb 6, 2017

where this code to be used??

@ankita-SML
Copy link

ankita-SML commented Apr 1, 2017

Please let me knw how and where to use this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment