欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-07-11 23:19:04
现在android中有许多的应用仿苹果的在应用图标上显示小红点。当然有着一些手机rom对小红点的支持,比如小米,三星等。google的api是没有提供这项工能的,这项功能一...

现在android中有许多的应用仿苹果的在应用图标上显示小红点。当然有着一些手机rom对小红点的支持,比如小米,三星等。google的api是没有提供这项工能的,这项功能一般都是厂商自己定制的,所以让开发者着实头痛,我也是弄了2天,不是所有的机型都可以.以后再一点点完善吧.希望对读文章的人,或也在受此困惑的人有点帮助!

效果图:(dodo那个是我的应用,小米miui8测试)

直接上代码吧,代码中有注释:

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

直接上代码吧,代码中有注释:

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

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

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

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