Android examples for Phone:Badge
reset Badge Count
//package com.java2s; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Build; import java.lang.reflect.Field; public class Main { public static void resetBadgeCount(Context context) { setBadgeCount(context, 0);//w w w.j av a 2 s . c o m } public static void setBadgeCount(Context context, int count) { if (count <= 0) { count = 0; } else { count = Math.max(0, Math.min(count, 999)); } if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) { sendToXiaoMi(context, count); } else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) { sendToSony(context, count); } else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) { sendToSamsung(context, count); } else { } } private static void sendToXiaoMi(Context context, int count) { try { Class miuiNotificationClass = Class .forName("android.app.MiuiNotification"); Object miuiNotification = miuiNotificationClass.newInstance(); Field field = miuiNotification.getClass().getDeclaredField( "messageCount"); field.setAccessible(true); field.set(miuiNotification, String.valueOf(count == 0 ? "" : count)); } catch (Exception e) { e.printStackTrace(); Intent localIntent = new Intent( "android.intent.action.APPLICATION_MESSAGE_UPDATE"); localIntent .putExtra( "android.intent.extra.update_application_component_name", context.getPackageName() + "/" + getLauncherClassName(context)); localIntent.putExtra( "android.intent.extra.update_application_message_text", String.valueOf(count == 0 ? "" : count)); context.sendBroadcast(localIntent); } } private static void sendToSony(Context context, int count) { String launcherClassName = getLauncherClassName(context); if (launcherClassName == null) { return; } boolean isShow = true; if (count == 0) { isShow = false; } Intent localIntent = new Intent(); localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE"); localIntent.putExtra( "com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", isShow); localIntent.putExtra( "com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName); localIntent.putExtra( "com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count)); localIntent.putExtra( "com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName()); context.sendBroadcast(localIntent); } private static void sendToSamsung(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); } /** * Retrieve launcher activity name of the application from the context * * @param context The context of the application package. * @return launcher activity name of this application. From the * "android:name" attribute. */ private static String getLauncherClassName(Context context) { PackageManager packageManager = context.getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.setPackage(context.getPackageName()); intent.addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); if (info == null) { info = packageManager.resolveActivity(intent, 0); } return info.activityInfo.name; } }