2016-07-31 110 views
2

我能夠顯示與下面的代碼三星設備與應用程序圖標徽章計數 -如何使用Redmi上的應用程序圖標顯示徽章數量?

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

但我不能夠達到這個上紅米手機設備,雖然Facebook的,WhatsApp的等能。如果您知道動作意圖和附加功能用於廣播,請告訴我。感謝你的幫助。

+0

做你能夠顯示在MI徽章?如果是的話,你能分享一下怎麼做嗎? –

回答

1

也許,用戶需要從設置中明確啓用該選項。查看此處以供參考:badge count on launcher icon

+1

感謝您的回覆。你是對的,他們可能必須啓用,但是我面臨的挑戰是我無法弄清楚我必須使用什麼權限字符串,以及我需要爲Redmi設置什麼意圖字符串。從一家制造商到另一家制造商,我發現三星,HTC和索尼的情況各不相同。找不到關於Redmi的任何有用的文檔。 –

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

這是從ShortcutBadger應用github上project link有關許可證(我沒有想法複製的代碼使用此代碼/權限)

Mi specific badge showing file code

相關問題