Android examples for Phone:Badge
set App Badge
//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 android.util.Log; public class Main { private static final String tag = "BadgeUtil"; public static void setAppBadge(Context context, int number) { context = context.getApplicationContext(); if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) { miuiSetAppBadge(context, number); } else if (Build.MANUFACTURER.equalsIgnoreCase("samsung")) { samsungSetAppBadge(context, number); }//from w w w.j a v a2s . c o m } public static void miuiSetAppBadge(Context context, int num) { Intent localIntent = new Intent( "android.intent.action.APPLICATION_MESSAGE_UPDATE"); localIntent.putExtra( "android.intent.extra.update_application_component_name", context.getPackageName() + "/" + getLaunchActivityName(context)); if (num > 0) { localIntent.putExtra( "android.intent.extra.update_application_message_text", String.valueOf(Math.min(num, 99))); } else { localIntent.putExtra( "android.intent.extra.update_application_message_text", ""); } context.sendBroadcast(localIntent); } private static void samsungSetAppBadge(Context context, int number) { number = Math.max(number, 0); number = Math.min(number, 99); String launcherClassName = getLaunchActivityName(context); if (launcherClassName == null) { return; } Intent intent = new Intent( "android.intent.action.BADGE_COUNT_UPDATE"); intent.putExtra("badge_count", number); intent.putExtra("badge_count_package_name", context.getPackageName()); intent.putExtra("badge_count_class_name", launcherClassName); context.sendBroadcast(intent); } private static String getLaunchActivityName(Context context) { PackageManager localPackageManager = context.getPackageManager(); Intent localIntent = new Intent("android.intent.action.MAIN"); localIntent.addCategory("android.intent.category.LAUNCHER"); for (ResolveInfo localResolveInfo : localPackageManager .queryIntentActivities(localIntent, 0)) { if (!localResolveInfo.activityInfo.applicationInfo.packageName .equalsIgnoreCase(context.getPackageName())) { continue; } return localResolveInfo.activityInfo.name; } return null; } }