当前位置: 移动技术网 > IT编程>移动开发>Android > Android 桌面图标右上角显示未读消息数字

Android 桌面图标右上角显示未读消息数字

2019年07月24日  | 移动技术网IT编程  | 我要评论

天赐我爱国语版,灵台二手房,阿有正传好看吗

背景:

在android原生系统中,众所周知不支持桌面图标显示未读消息提醒的数字,虽然第三方控件badgeview可以实现应用内的数字提醒。但对于系统的图标,特别是app的logo图标很难实现数字标志,即使是绘图的方式不断修改,但这种方式天生弊端,实用性很差。但幸运的是,一些强大的手机厂商(小米,三星,索尼)提供了私有的api,但也带来了难度,api的不同就意味着代码量的增加和兼容性问题更加突出。

现在我们来看看他们是如何实现的:

实现原理:

首先我们要明白 并不是应用本身处理对启动图标进行修改、图标的动态修改的过程主要是在launcher里面完成的.在应用安装,更新,卸载的时候,都会有广播发出,launcher在launcherapplication 中注册广播,在launchermodel中处理接收到广播的消息,重新加载更新应用信息(如:应用图标、文字等)。但是原生的android系统是并不支持该特性的(及不能通过发送特定的系统广播 达到动态修改启动图标的效果),但是在强大的第三方android手机厂商(如:三星、小米)的系统源码深度定制下、通过修改了launcher源代码,增加/注册了新的广播接收器用来接收应用发送来的未读消息数广播,接收到广播后,系统将未读消息的数目显示事件交给launcher去处理,调用相关方法去重绘应用的icon,最终达到动态更新应用图标的效果。

示例代码:

public class launcherbadgehelper {

 /**
  * set badge count

  * 针对 samsung / xiaomi / sony 手机有效
  *
  * @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 {
   sendtosamsumg(context, count);
  }
 }

 /**
  * 向小米手机发送未读消息数广播
  *
  * @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)); // 设置信息数-->这种发送必须是miui 6才行
  } catch (exception e) {
   logcontroller.e(e.tostring());
   // 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);
  }
 }

 /**
  * 向索尼手机发送未读消息数广播

  * 据说:需添加权限:<uses-permission android:name="com.sonyericsson.home.permission.broadcast_badge"> [未验证]
  *
  * @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));//数字
  localintent.putextra("com.sonyericsson.home.intent.extra.badge.package_name", context.getpackagename());//包名
  context.sendbroadcast(localintent);
 }

 /**
  * 向三星手机发送未读消息数广播
  *
  * @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未读显示数

  *
  * @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;
 }
}</uses-permission>

可以看出小米,三星,索尼处理方式都是通过发送广播来实现的。

但是:小米miui6以后,改变了处理方式,弃用了发送广播的方式,改为通过发送通知。

一、基本介绍

1、默认的情况

当app 向通知栏发送了一条通知 (通知不带进度条并且用户可以删除的),那么桌面app icon角标就会显示1.此时app显示的角标数是和通知栏里app发送的通知数对应的,即向通知栏发送了多少通知就会显示多少角标

二、实现代码

第三方app需要用反射来调用,参考代码:

notificationmanager mnotificationmanager = (notificationmanager) this

.getsystemservice(context.notification_service);

notification.builder builder = new notification.builder(this)

.setcontenttitle(“title”).setcontenttext(“text”).setsmallicon(r.drawable.icon);

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

} catch (exception e) {

e.printstacktrace();

}
mnotificationmanager.notify(0,notification);

自己在之前的代码根据官方代码总结新的方法如下:

/**
 * 向小米手机发送未读消息数广播miui6以后
 *
 * @param count
 */
 private static void sendtoxiaomi2(context context, int count) {
  notificationmanager mnotificationmanager = (notificationmanager) myapplication.getcontext().getsystemservice(context.notification_service);
  notification.builder builder = new notification.builder(myapplication.getcontext()).setcontenttitle("title").setcontenttext("text").setsmallicon(r.drawable.ico_haoyilogo);
  notification notification = null;
  if (android.os.build.version.sdk_int >= android.os.build.version_codes.jelly_bean) {
   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);
   mnotificationmanager.notify(10, notification);
  } catch (exception e) {
   e.printstacktrace();
   logcontroller.e(e.tostring());
   // 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);
  }
 }

这样既能兼容miui6之前的,还能实现miui6以后的。自己在开发的时候经过测试,发现miui内部对于相同的消息数字是不显示的,由于我测试的时候用的是写死的数字,导致走了很多弯路。还有,自己在查找资料的时候发现有许多朋友都遇到过这样的问题,未读消息数字只有在第一次安装的时候才显示,进入后再设置就没有了,我估计都是因为数字相同造成的。

细想一下,miui这种做法也挺好的,消息数字和通知绑定,当来通知时触发事件,从而桌面图标数字动态改变。当我们清楚通知时,清空数字。自己也调研了ios的做法,他们只是通过调用系统的一个方法将消息数字传进去即可,做法类似于android 通过发送广播方式,和三星一样。

那么,小米是如何做到累加的呢。

我们只需定义全局变量count,初始值设置为1,然后发送通知后,手动改变count值,count=count+1。

友情链接:

https://github.com/lixiangers/badgeutil

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网