Android 應(yīng)用啟動圖標(biāo)未讀消息數(shù)顯示

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;

import java.lang.reflect.Field;

/**

  • 應(yīng)用啟動圖標(biāo)未讀消息數(shù)顯示 工具類 (效果如:QQ、微信、未讀短信 等應(yīng)用圖標(biāo))

  • 依賴于第三方手機(jī)廠商(如:小米、三星)的Launcher定制、原生系統(tǒng)不支持該特性

  • 該工具類支持的設(shè)備有小米、三星、索尼【其中小米、三星親測有效、索尼未驗(yàn)證】

  • @author junzhitian
    */
    public class BadgeUtil {

    /**

    • Set badge count

    • 針對 Samsung / xiaomi / sony 手機(jī)有效

    • @param context The context of the application package.

    • @param count Badge count to be set
      */
      public static void setBadgeCount(Context context, int count) {
      if (count <= 0) {
      count = 0;
      } else {
      count = Math.max(0, Math.min(count, 99));
      }

      if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
      sendToXiaoMi(context, count);
      } else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {
      sendToSony(context, count);
      } else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {
      sendToSamsumg(context, count);
      } else {
      // Toast.makeText(context, "Not Support", Toast.LENGTH_LONG).show();
      }
      }

    /**

    • 向小米手機(jī)發(fā)送未讀消息數(shù)廣播
    • @param count
      */
      private static void sendToXiaoMi(Context context, int count) {
      try {
      Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
      Object miuiNotification = miuiNotificationClass.newInstance();
      Field field = miuiNotification.getClass().getDeclaredField("messageCount");
      field.setAccessible(true);
      field.set(miuiNotification, String.valueOf(count == 0 ? "" : count)); // 設(shè)置信息數(shù)-->這種發(fā)送必須是miui6才行
      } catch (Exception e) {
      e.printStackTrace();
      // miui 6之前的版本
      Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
      localIntent.putExtra("android.intent.extra.update_application_component_name", context.getPackageName() + "/" + getLauncherClassName(context));
      localIntent.putExtra("android.intent.extra.update_application_message_text", String.valueOf(count == 0 ? "" : count));
      context.sendBroadcast(localIntent);
      }
      }

    /**

    • 向索尼手機(jī)發(fā)送未讀消息數(shù)廣播

    • 據(jù)說:需添加權(quán)限:<uses-permission

    • android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /> [未驗(yàn)證]

    • @param count
      */
      private static void sendToSony(Context context, int count) {
      String launcherClassName = getLauncherClassName(context);
      if (launcherClassName == null) {
      return;
      }

      boolean isShow = true;
      if (count == 0) {
      isShow = false;
      }
      Intent localIntent = new Intent();
      localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
      localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", isShow);// 是否顯示
      localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);// 啟動頁
      localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));// 數(shù)字
      localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());// 包名
      context.sendBroadcast(localIntent);
      }

    /**

    • 向三星手機(jī)發(fā)送未讀消息數(shù)廣播
    • @param count
      */
      private static void sendToSamsumg(Context context, int count) {
      String launcherClassName = getLauncherClassName(context);
      if (launcherClassName == null) {
      return;
      }
      Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
      intent.putExtra("badge_count", count);
      intent.putExtra("badge_count_package_name", context.getPackageName());
      intent.putExtra("badge_count_class_name", launcherClassName);
      context.sendBroadcast(intent);
      }

    /**

    • 重置、清除Badge未讀顯示數(shù)
    • @param context
      */
      public static void resetBadgeCount(Context context) {
      setBadgeCount(context, 0);
      }

    /**

    • Retrieve launcher activity name of the application from the context

    • @param context The context of the application package.

    • @return launcher activity name of this application. From the

    • "android:name" attribute.
      */
      private static String getLauncherClassName(Context context) {
      PackageManager packageManager = context.getPackageManager();

      Intent intent = new Intent(Intent.ACTION_MAIN);
      // To limit the components this Intent will resolve to, by setting an
      // explicit package name.
      intent.setPackage(context.getPackageName());
      intent.addCategory(Intent.CATEGORY_LAUNCHER);

      // All Application must have 1 Activity at least.
      // Launcher activity must be found!
      ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

      // get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER
      // if there is no Activity which has filtered by CATEGORY_DEFAULT
      if (info == null) {
      info = packageManager.resolveActivity(intent, 0);
      }
      return info.activityInfo.name;
      }
      }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,233評論 25 708
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,363評論 0 17
  • 親愛的佶: 今天的語文測驗(yàn)?zāi)阕晕腋杏X良好,激動地對我說:選擇題和同學(xué)對過了,對得挺多,這次翻車不太可能。哈哈...
    珠珠米閱讀 123評論 0 3
  • 大家好!周末君從本周開始,將于每周周三周五帶來【周末逛點(diǎn)別的吧】欄目,為大家網(wǎng)羅最新的好玩去處,以便于大家周末吃飯...
    周末干啥閱讀 6,130評論 15 24
  • 上輔導(dǎo)課的時候,認(rèn)識一個姑娘,我都叫她小袁。膚白,脾氣溫和,還有點(diǎn)呆萌呆萌的。我比她大了七歲,她今年上大一,師范數(shù)...
    蘇清長閱讀 185評論 0 0

友情鏈接更多精彩內(nèi)容