你也可以查看我的其他同类文章,也会让你有一定的收货
Android系统 小米,三星,索尼手机发送桌面快键提醒数字图标,在Android系统中,众所周知不支持BadgeNumber,虽然第三方控件BadgeView可以实现应用内的数字提醒。
但对于系统的图标,特别是app的logo图标很难实现数字标志,即使是绘图的方式不断修改,但这种方式天生弊端,实用性很差。但幸运的是,某些ROM厂商提供了私有的API,但也带来了难度,API的不同意意味着代码量的增加和兼容性问题更加突出。
我们现在来实现桌面logo或者说icon右上角的图标,先来看2张图,第一张来自互联网,第二张来自个人实践!(由于实验条件有限,只能测试小米的(⊙o⊙)…,有兴趣的同学测试一下其他的吧)

实现原理:
首先我们要明白 并不是应用本身处理对启动图标进行修改、图标的动态修改的过程主要是在Launcher里面完成的.在应用安装,更新,卸载的时候,都会有广播发出,Launcher在LauncherApplication 中注册广播,在LauncherModel中处理接收到广播的消息,重新加载更新应用信息(如:应用图标、文字等)。但是原生的android系统是并不支持该特性的(即不能通过发送特定的系统广播 达到动态修改启动图标的效果),但是在强大的第三方Android手机厂商(如:三星、小米)的系统源码深度定制下、通过修改了Launcher源代码,增加/注册了新的广播接收器用来接收应用发送来的未读消息数广播,接收到广播后,系统将未读消息的数目显示事件交给Launcher去处理,调用相关方法去重绘应用的icon,最终达到动态更新应用图标的效果。
MIUI 桌面角标官方开源代码简介
参考:http://dev.xiaomi.com/doc/p=3904/index.html
MIUI 6上重新设计了桌面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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
显示角标工具类
注意:示例源代码中MIUI系统的功能实现和官方介绍的方法不一样,工具类使用的是官方介绍的方法
这个工具类,参考的这篇博客的MIUI系统测试无效,我改了一下,小米测试可以。sony和三星未测试
工具类下载地址:http://download.youkuaiyun.com/detail/xx326664162/9484065
源代码
package cn.hdnc.BadgeUtil;
import android.app.Notification;
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;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 应用启动图标未读消息数显示 工具类 (效果如: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(Notification notification,Context context, int count) {
if (count <= 0) {
count = 0;
} else {
count = Math.max(0, Math.min(count, 99));
}
if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
sendToXiaoMi(notification, 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(Notification notification,Context context, int count) {
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();
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(Notification notification,Context context) {
setBadgeCount(notification, 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);
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
拓展
https://github.com/leolin310148/ShortcutBadger克隆这个项目,可能会碰到如下两个问题:
http://blog.youkuaiyun.com/xx326664162/article/details/51083400
http://blog.youkuaiyun.com/xx326664162/article/details/51087827
测试发现小米手机不能实现角标功能,其它手机暂未测试
又在网上搜索了好久,感觉这个还不错,还没有亲测
http://www.jianshu.com/p/0992ff9eeeb6
参考:
http://my.oschina.net/ososchina/blog/352286?p=1#comments
http://www.voidcn.com/blog/kongbaidepao/article/p-62251.html
http://www.eoeandroid.com/thread-557210-1-1.html
转载至:http://blog.youkuaiyun.com/xx326664162/article/details/51082574