Android App 圖標Number設置
源碼獲取地址 :
獲取手機注冊名稱
static {
//硬件制造商
String manufacturer = Build.MANUFACTURER;
if (manufacturer.equalsIgnoreCase(MobileBrand.HUAWEI)) {
IMPL = new ImplHuaWei();
} else if (manufacturer.equalsIgnoreCase(MobileBrand.XIAOMI)) {
IMPL = new ImplXiaoMi();
} else if (manufacturer.equalsIgnoreCase(MobileBrand.VIVO)) {
IMPL = new ImplVIVO();
} else if (manufacturer.equalsIgnoreCase(MobileBrand.OPPO)) {
IMPL = new ImplOPPO();
} else {
IMPL = new ImplBase();
}
}
分別為對應的手機設置角標
-
華為
/** * 設置應用的桌面角標,已在一些華為手機上測試通過,但是無法保證在所有華為手機上都生效 * * @param context context * @param number 角標顯示的數(shù)字 * 1. 使用 Bundle 傳輸數(shù)據(jù), * 包括 packageName , 當前APP 啟動的Activity的className, * 以及需要設置角標的個數(shù)badgenumber , * * 2. 使用內(nèi)容觀察者更新數(shù)據(jù). */ public static void setBadgeNumber(Context context, int number) { try { if (number < 0) number = 0; Bundle bundle = new Bundle(); bundle.putString("package", context.getPackageName()); String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName(); bundle.putString("class", launchClassName); bundle.putInt("badgenumber", number); context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bundle); } catch (Exception e) { e.printStackTrace(); } }
-
VIVO
/** * 設置應用的桌面角標 * * @param context context * @param number 角標顯示的數(shù)字 * * 1. 使用 Intent 傳輸數(shù)據(jù), new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM"); * 包括 packageName , 當前APP 啟動的Activity的className, * 以及需要設置角標的個數(shù) notificationNum , * * 2. 使用廣播將 intent 發(fā)送出去. */ public static void setBadgeNumber(Context context, int number) { try { Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM"); intent.putExtra("packageName", context.getPackageName()); String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName(); intent.putExtra("className", launchClassName); intent.putExtra("notificationNum", number); context.sendBroadcast(intent); } catch (Exception e) { e.printStackTrace(); } } public static void setBadgeNumber(Context context, int number) { try { Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM"); intent.putExtra("packageName", context.getPackageName()); String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName(); intent.putExtra("className", launchClassName); intent.putExtra("notificationNum", number); context.sendBroadcast(intent); } catch (Exception e) { e.printStackTrace(); } }
-
OPPO
/** * 設置應用的桌面角標 * * @param context context * @param number 角標顯示的數(shù)字 * * 1. 使用 Intent 傳輸數(shù)據(jù), new Intent("com.oppo.unsettledevent"); * * 以及需要設置角標的個數(shù) number / upgradeNumber, * 通過檢查是否配置了對應的廣播接受者,來考慮使用廣播發(fā)送數(shù)據(jù), * 和使用對應的內(nèi)容提供者發(fā)送數(shù)據(jù), 包含對應的count. * * 2. 使用廣播將 intent 發(fā)送出去. */ public static void setBadgeNumber(Context context, int number) { try { if (number == 0) { number = -1; } Intent intent = new Intent("com.oppo.unsettledevent"); intent.putExtra("pakeageName", context.getPackageName()); intent.putExtra("number", number); intent.putExtra("upgradeNumber", number); if (canResolveBroadcast(context, intent)) { context.sendBroadcast(intent); } else { try { Bundle extras = new Bundle(); extras.putInt("app_badge_count", number); context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"), "setAppBadgeCount", null, extras); } catch (Throwable th) { Log.e("OPPO" + " Badge error", "unable to resolve intent: " + intent.toString()); } } } catch (Exception e) { e.printStackTrace(); Log.e("OPPO" + " Badge error", "set Badge failed"); } } public static boolean canResolveBroadcast(Context context, Intent intent) { PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0); return receivers != null && receivers.size() > 0; }
-
XIAOMI
/** * 單獨對小米進行設置. * */ public static void setBadgeNumber(Notification notification, int number) { try { Field field = notification.getClass().getDeclaredField("extraNotification"); Object extraNotification = field.get(notification); Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class); method.invoke(extraNotification, number); } catch (Exception e) { e.printStackTrace(); } }
- 設置角標函數(shù).
private void setXiaomiBadgeNumber() {
NotificationManager notificationManager = (NotificationManager) MainActivity.this.
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(MainActivity.this.getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setContentTitle("推送標題")
.setContentText("我是推送內(nèi)容")
.setTicker("ticker")
.setAutoCancel(true)
.build();
//相鄰的兩次角標設置如果數(shù)字相同的話,好像下一次會不生效
BadgeNumberManagerXiaoMi.setBadgeNumber(notification,mCount++);
notificationManager.notify(1000, notification);
Toast.makeText(MainActivity.this, "設置桌面角標成功", Toast.LENGTH_SHORT).show();
}
//設置角標 (小米除外)
BadgeNumberManager.from(MainActivity.this).setBadgeNumber(0);
//清除角標
BadgeNumberManager.from(MainActivity.this).setBadgeNumber(0);
?