您的位置:首页 > 移动开发 > Android开发

Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒

2015-03-17 16:50 936 查看
在Android手机上,如QQ、微信当有未读消息的时候、我们可以看到在应用的启动图标的右上角会有一个红色圈圈、且圈圈里会动态显示未读消息的数目,如下图显示:



那么该功能是怎么实现的呢?

在万能的互联网搜索和翻阅了大量相关资料、也请教了一些技术群里的大咖们。从他们那里我获知、提取了一些关键词:第三方控件BadgeView(实现应用内的数字提醒)、快捷图标、Launcher、反射。

零零碎碎的花费了近一天时间、终于算是弄明白了。写了个demo测试程序 验证并自测了一下。 demo效果如下所示:

三星Galaxy S4上测试效果如下:



小米手机上测试效果如下:



实现原理:

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

在了解了实现原理之后、我们大概明白整个流程是这样的(原生系统除外):

在第三方手机制造商的ROM下、如果修改了Launcher源码且支持了上面所说的未读消息数广播的接收、那么我们只要在应用中发送一条能让系统接收的广播就可以在这种设备的手机上实现本篇想要达到的效果。

但是第三方手机制造商们的这种广播的接收的条件肯定是各不相同的、因此最关键的就是要知道各手机制造商的这种广播的Intent接收条件。

幸运的是 在万能的互联网上 总能找到你需要的东西,下面封装了一个工具类 BadgeUtil.java 实现了不同手机制造商的未读消息数目广播。具体代码如下:

import java.lang.reflect.Field;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.widget.Toast;

/**
* 应用启动图标未读消息数显示 工具类  (效果如:QQ、微信、未读短信 等应用图标)<br/>
* 依赖于第三方手机厂商(如:小米、三星)的Launcher定制、原生系统不支持该特性<br/>
* 该工具类 支持的设备有 小米、三星、索尼【其中小米、三星亲测有效、索尼未验证】
* @author ice_zhengbin@163.com
*
*/
public class BadgeUtil {

/**
* Set badge count<br/>
* 针对 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 {
Toast.makeText(context, "Not Support", Toast.LENGTH_LONG).show();
}
}

/**
* 向小米手机发送未读消息数广播
* @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) {
e.printStackTrace();
// 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);
}
}

/**
* 向索尼手机发送未读消息数广播<br/>
* 据说:需添加权限:<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未读显示数<br/>
* @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;
}

}


在启动的Activity中、发送未读消息数目广播 和 重置/清除未读消息数目广播 的调用如下:

// 发送未读消息数目广播:count为未读消息数目(int类型)

BadgeUtil.setBadgeCount(getApplicationContext(), count);


// 发送重置/清除未读消息数目广播:

BadgeUtil.resetBadgeCount(getApplicationContext());


资料参考:

http://blog.csdn.net/andylao62/article/details/41794695

http://blog.csdn.net/wx_962464/article/details/37997299

https://github.com/ekinlyw/android-badge

http://www.tuicool.com/articles/JV7vIr

—————————————————————————————————————

如果文章内容对您有帮助, 可以帮 顶 一下,来支持一下哦!

如果您对文章内容有任何疑问或有更好的见解, 欢迎通过留言或发邮件的方式联系我:

ice_zhengbin@163.com

如需要转载,请注明出处,谢谢!!

—————————————————————————————————————
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐