全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Android应用图标上的小红点Badge实践代码

现在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 没有效果.

至于锤子,魅族,金立等,网上说是不支持此功能的,,因为没有测试机,并没有验证.

乐视:有自带效果(没有询问客服,就不给人添乱了!)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# android  # 图标小红点  # android应用小红点  # 小红点  # Android中TabLayout添加小红点的示例代码  # Android实现消息提醒小红点效果  # Android高仿QQ小红点功能  # Android仿QQ未读消息--红点拖拽删除【源代码】  # Android 未读消息的红点显示  # Android带数字或红点的底部导航拦和联网等待加载动画示例  # Android BadgeView红点更新信息提示示例代码  # Android开发中TextView 实现右上角跟随文本动态追加圆形红点  # Android自定义ActionProvider ToolBar实现Menu小红点  # Android 基于MediatorLiveData实现红点的统一管理  # 华为  # 中有  # 的人  # 都是  # 索尼  # 魅族  # 星等  # 就不  # 客服  # 也在  # 其他的  # 给人  # 不支持  # 可以实现  # 自带  # 弄了  # 大家多多  # 说对  # 受此 


相关文章: 建站之星代理如何优化在线客服效率?  如何在IIS7上新建站点并设置安全权限?  Dapper的Execute方法的返回值是什么意思 Dapper Execute返回值详解  如何在建站之星绑定自定义域名?  建站之星后台密码遗忘或太弱?如何重置与强化?  广州建站公司哪家好?十大优质服务商推荐  高性能网站服务器部署指南:稳定运行与安全配置优化方案  Python多线程使用规范_线程安全解析【教程】  建站主机选择指南:服务器配置与SEO优化实战技巧  建站之星如何取消后台验证码生成?  建站org新手必看:2024最新搭建流程与模板选择技巧  ,怎么用自己头像做动态表情包?  网站制作知乎推荐,想做自己的网站用什么工具比较好?  娃派WAP自助建站:免费模板+移动优化,快速打造专业网站  免费网站制作appp,免费制作app哪个平台好?  武汉网站如何制作,黄黄高铁武穴北站途经哪些村庄?  广州网站建站公司选择指南:建站流程与SEO优化关键词解析  C++如何将C风格字符串(char*)转换为std::string?(代码示例)  中山网站制作网页,中山新生登记系统登记流程?  电脑免费海报制作网站推荐,招聘海报哪个网站多?  洛阳网站制作公司有哪些,洛阳的招聘网站都有哪些?  C#如何在一个XML文件中查找并替换文本内容  如何设计高效校园网站?  如何制作算命网站,怎么注册算命网站?  广平建站公司哪家专业可靠?如何选择?  专业网站建设制作报价,网页设计制作要考什么证?  如何选择适合PHP云建站的开源框架?  如何在阿里云ECS服务器部署织梦CMS网站?  XML的“混合内容”是什么 怎么用DTD或XSD定义  网站app免费制作软件,能免费看各大网站视频的手机app?  公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?  如何通过wdcp面板快速创建网站?  如何自定义建站之星网站的导航菜单样式?  江苏网站制作公司有哪些,江苏书法考级官方网站?  如何获取PHP WAP自助建站系统源码?  建站VPS推荐:2025年高性能服务器配置指南  建站之星价格显示格式升级,你的预算足够吗?  如何使用Golang安装API文档生成工具_快速生成接口文档  重庆市网站制作公司,重庆招聘网站哪个好?  网站制作员失业,怎样查看自己网站的注册者?  已有域名和空间,如何快速搭建网站?  如何快速使用云服务器搭建个人网站?  高防服务器租用首荐平台,企业级优惠套餐快速部署  建站之星安装步骤有哪些常见问题?  成都网站制作价格表,现在成都广电的单独网络宽带有多少的,资费是什么情况呢?  湖州网站制作公司有哪些,浙江中蓝新能源公司官网?  网站制作话术技巧,网站推广做的好怎么话术?  如何在Golang中使用replace替换模块_指定本地或远程路径  网站建设制作、微信公众号,公明人民医院怎么在网上预约?  建站之星安装失败:服务器环境不兼容? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。