国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android應用圖標上的小紅點Badge實踐代碼

2019-12-12 02:23:10
字體:
來源:轉載
供稿:網友

現在Android中有許多的應用仿蘋果的在應用圖標上顯示小紅點。當然有著一些手機ROM對小紅點的支持,比如小米,三星等。google的api是沒有提供這項工能的,這項功能一般都是廠商自己定制的,所以讓開發者著實頭痛,我也是弄了2天,不是所有的機型都可以.以后再一點點完善吧.希望對讀文章的人,或也在受此困惑的人有點幫助!

效果圖:(DODO那個是我的應用,小米miui8測試)

直接上代碼吧,代碼中有注釋:

直接上代碼吧,代碼中有注釋:

BadgeUtil類:

public class BadgeUtil {  private BadgeUtil() throws InstantiationException {    throw new InstantiationException("This class is not for instantiation");  }  /**   * 設置Badge 目前支持Launcher   */  public static void setBadgeCount(Context context, int count, int iconResId) {    if (count <= 0) {      count = 0;    } else {      count = Math.max(0, Math.min(count, 99));    }    if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) {      setBadgeOfMIUI(context, count, iconResId);    } else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {      setBadgeOfSony(context, count);    } else if (Build.MANUFACTURER.toLowerCase().contains("samsung") ||        Build.MANUFACTURER.toLowerCase().contains("lg")) {      setBadgeOfSumsung(context, count);    } else if (Build.MANUFACTURER.toLowerCase().contains("htc")) {      setBadgeOfHTC(context, count);    } else if (Build.MANUFACTURER.toLowerCase().contains("nova")) {      setBadgeOfNova(context, count);    }else if (Build.MANUFACTURER.toLowerCase().contains("OPPO")) {//oppo      //setBadgeOfOPPO(context, count);    }else if (Build.MANUFACTURER.toLowerCase().contains("LeMobile")) {//樂視    }    else if (Build.MANUFACTURER.toLowerCase().contains("vivo")) {      setBadgeOfVIVO(context, count);    }else if (Build.MANUFACTURER.toLowerCase().contains("HUAWEI")||Build.BRAND.equals("Huawei")||Build.BRAND.equals("HONOR")) {//華為      setHuaweiBadge(context, count);    }else if (Build.MANUFACTURER.toLowerCase().contains("")) {//魅族    }else if (Build.MANUFACTURER.toLowerCase().contains("")) {//金立    }else if (Build.MANUFACTURER.toLowerCase().contains("")) {//錘子    }else {      //Toast.makeText(context, "Not Found Support Launcher", Toast.LENGTH_LONG).show();    }  }  /**   * 設置MIUI的Badge   */  private static void setBadgeOfMIUI(Context context, int count, int iconResId) {    NotificationManager mNotificationManager = (NotificationManager) context        .getSystemService(Context.NOTIFICATION_SERVICE);    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);    builder.setContentTitle("標題").setContentText("消息正文").setSmallIcon(iconResId);    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, count);    } catch (Exception e) {      e.printStackTrace();    }    mNotificationManager.notify(0, notification);  }  /**   * 設置索尼的Badge   * 需添加權限:<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />   */  private static void setBadgeOfSony(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));//數字    localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名    context.sendBroadcast(localIntent);  }  /**   * 設置三星的Badge/設置LG的Badge   */  private static void setBadgeOfSumsung(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);  }  /**   * 設置HTC的Badge   */  private static void setBadgeOfHTC(Context context, int count) {    Intent intentNotification = new Intent("com.htc.launcher.action.SET_NOTIFICATION");    ComponentName localComponentName = new ComponentName(context.getPackageName(), getLauncherClassName(context));    intentNotification.putExtra("com.htc.launcher.extra.COMPONENT", localComponentName.flattenToShortString());    intentNotification.putExtra("com.htc.launcher.extra.COUNT", count);    context.sendBroadcast(intentNotification);    Intent intentShortcut = new Intent("com.htc.launcher.action.UPDATE_SHORTCUT");    intentShortcut.putExtra("packagename", context.getPackageName());    intentShortcut.putExtra("count", count);    context.sendBroadcast(intentShortcut);  }  /**   * 設置Nova的Badge   */  private static void setBadgeOfNova(Context context, int count) {    ContentValues contentValues = new ContentValues();    contentValues.put("tag", context.getPackageName() + "/" + getLauncherClassName(context));    contentValues.put("count", count);    context.getContentResolver().insert(Uri.parse("content://com.teslacoilsw.notifier/unread_count"),        contentValues);  }  /**   * 設置vivo的Badge :vivoXplay5 vivo x7無效果   */  private static void setBadgeOfVIVO(Context context,int count){    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", count);      context.sendBroadcast(intent);    }catch (Exception e){      e.printStackTrace();    }  }  /**   *設置oppo的Badge :oppo角標提醒目前只針對內部軟件還有微信、QQ開放,其他的暫時無法提供   */  private static void setBadgeOfOPPO(Context context,int count){    try {      Bundle extras = new Bundle();      extras.putInt("app_badge_count", count);      context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"), "setAppBadgeCount", String.valueOf(count), extras);    } catch (Exception e) {      e.printStackTrace();    }  }  /**   * 設置華為的Badge :mate8 和華為 p7,honor暢玩系列可以,honor6plus 無效果   */  public static void setHuaweiBadge(Context context, int count)  {    try {      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", count);      context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bundle);    } catch (Exception e) {      e.printStackTrace();    }  }  public static void setBadgeOfMadMode(Context context, int count, String packageName, String className) {    Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");    intent.putExtra("badge_count", count);    intent.putExtra("badge_count_package_name", packageName);    intent.putExtra("badge_count_class_name", className);    context.sendBroadcast(intent);  }  /**   * 重置Badge   */  public static void resetBadgeCount(Context context, int iconResId) {    setBadgeCount(context, 0, iconResId);  }  public static String getLauncherClassName(Context context) {    PackageManager packageManager = context.getPackageManager();    Intent intent = new Intent(Intent.ACTION_MAIN);    intent.setPackage(context.getPackageName());    intent.addCategory(Intent.CATEGORY_LAUNCHER);    ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);    if (info == null) {      info = packageManager.resolveActivity(intent, 0);    }    return info.activityInfo.name;  }}

總結:

小米,三星,索尼,htc,朵唯.手中有的手機基本小紅點功能都是沒有問題的.

oppo:廠商不提供此功能,只對于市面上排名前5的商用聊天提供,還說對企業內部的im會提供此功能,不過必須要集成oppo證書.

華為:與oppo說法一樣,但是按上面代碼測試,華為的部分機型是可以實現的.榮耀6和6p沒有效果.

vivo:部分手機有效果,vivo xplay5 x7 沒有效果.

至于錘子,魅族,金立等,網上說是不支持此功能的,,因為沒有測試機,并沒有驗證.

樂視:有自帶效果(沒有詢問客服,就不給人添亂了!)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 牙克石市| 富阳市| 平定县| 鄂托克前旗| 黔江区| 朝阳区| 崇礼县| 芜湖市| 文昌市| 阿拉善盟| 盐城市| 武安市| 鄯善县| 黄陵县| 南康市| 博湖县| 新邵县| 台湾省| 莱芜市| 磐石市| 延边| 富顺县| 邢台市| 贡觉县| 商洛市| 邓州市| 营山县| 特克斯县| 德州市| 福州市| 菏泽市| 钟祥市| 鄢陵县| 莒南县| 垣曲县| 久治县| 海盐县| 安陆市| 光山县| 米脂县| 马关县|