如何在Redmi上显示带有应用程序图标的徽章数量?

时间:2016-07-31 15:32:36

标签: android badge

我能够在三星设备上使用应用程序图标显示徽章计数,并带有以下代码 -

<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />

    Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
    intent.putExtra("badge_count", badgeCount);
    intent.putExtra("badge_count_package_name", componentName.getPackageName());
    intent.putExtra("badge_count_class_name", componentName.getClassName());
sendBroadcast(intent);

但是我无法在Redmi设备上实现这一点,尽管Facebook,Whatsapp等能够实现。如果您了解动作意图和用于广播的附加内容,请通知我。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

也许,用户需要从设置中明确启用该选项。点击此处查看:badge count on launcher icon

答案 1 :(得分:0)

在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);
        }
    }
}

这是project link ShortcutBadger应用程序的 github 的复制代码(我不知道使用此代码的许可/权限)

Mi specific badge showing file code