代碼來顯示徽章MI/Xiomi手機
public static final String INTENT_ACTION = "android.intent.action.APPLICATION_MESSAGE_UPDATE";
public static final String EXTRA_UPDATE_APP_COMPONENT_NAME = "android.intent.extra.update_application_component_name";
public static final String EXTRA_UPDATE_APP_MSG_TEXT = "android.intent.extra.update_application_message_text";
private ResolveInfo resolveInfo;
@Override
public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
try {
Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
Object miuiNotification = miuiNotificationClass.newInstance();
Field field = miuiNotification.getClass().getDeclaredField("messageCount");
field.setAccessible(true);
try {
field.set(miuiNotification, String.valueOf(badgeCount == 0 ? "" : badgeCount));
} catch (Exception e) {
field.set(miuiNotification, badgeCount);
}
} catch (Exception e) {
Intent localIntent = new Intent(
INTENT_ACTION);
localIntent.putExtra(EXTRA_UPDATE_APP_COMPONENT_NAME, componentName.getPackageName() + "/" + componentName.getClassName());
localIntent.putExtra(EXTRA_UPDATE_APP_MSG_TEXT, String.valueOf(badgeCount == 0 ? "" : badgeCount));
if (BroadcastHelper.canResolveBroadcast(context, localIntent)) {
context.sendBroadcast(localIntent);
}
}
if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
tryNewMiuiBadge(context, badgeCount);
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void tryNewMiuiBadge(Context context, int badgeCount) throws ShortcutBadgeException {
if (resolveInfo == null) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
}
if (resolveInfo != null) {
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle("")
.setContentText("")
.setSmallIcon(resolveInfo.getIconResource());
Notification notification = builder.build();
try {
Field field = notification.getClass().getDeclaredField("extraNotification");
Object extraNotification = field.get(notification);
Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
method.invoke(extraNotification, badgeCount);
mNotificationManager.notify(0, notification);
} catch (Exception e) {
throw new ShortcutBadgeException("not able to set badge", e);
}
}
}
這是從ShortcutBadger
應用github上上project link有關許可證(我沒有想法複製的代碼使用此代碼/權限)
Mi specific badge showing file code
做你能夠顯示在MI徽章?如果是的話,你能分享一下怎麼做嗎? –