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 角标显示的数字 * 1. 使用 Bundle 传输数据, * 包括 packageName , 当前APP 启动的Activity的className, * 以及需要设置角标的个数badgenumber , * * 2. 使用内容观察者更新数据. */ 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 角标显示的数字 * * 1. 使用 Intent 传输数据, new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM"); * 包括 packageName , 当前APP 启动的Activity的className, * 以及需要设置角标的个数 notificationNum , * * 2. 使用广播将 intent 发送出去. */ 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 角标显示的数字 * * 1. 使用 Intent 传输数据, new Intent("com.oppo.unsettledevent"); * * 以及需要设置角标的个数 number / upgradeNumber, * 通过检查是否配置了对应的广播接受者,来考虑使用广播发送数据, * 和使用对应的内容提供者发送数据, 包含对应的count. * * 2. 使用广播将 intent 发送出去. */ 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(); } }
- 设置角标函数.
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("我是推送内容")
.setTicker("ticker")
.setAutoCancel(true)
.build();
//相邻的两次角标设置如果数字相同的话,好像下一次会不生效
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);