Android App 圖標Number設置

Android App 圖標Number設置

源碼獲取地址 :

https://github.com/beiliao-mobile/BadgeNumberManager

獲取手機注冊名稱

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

?

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容