桌面图标未读消息(小米,sony,三星手机)
新消息来了,在桌面的Laucher图标上显示新消息数
/**
* 应用桌面图标未读消息显示工具类
* 只支持 小米,三星和索尼
*/
public class BadgeUtil {
final static String LAUNCHER_ACTIVITY_NAME = "com.wenki.example.activity.SplashActivity";
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("sony")) {
sendToSony(context, count);
} else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {
sendToSamsumg(context, count);
} else if (isMIUI()) {
sendToXiaoMi(context, count);
} else {
LogUtils.d("BadgeUtil : Badge not support");
}
}
public static void clearBadgeCount(Context context) {
setBadgeCount(context, 0);
}
private static void sendToXiaoMi(Context context, int count) {
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle("Title").setContentText("Content Text").setSmallIcon(R.drawable.app_icon);
try {
Notification notification = builder.getNotification();
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(1001, notification);
} catch (Exception e) {
Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
localIntent.putExtra("android.intent.extra.update_application_component_name",
context.getPackageName() + "/" + LAUNCHER_ACTIVITY_NAME);
localIntent.putExtra("android.intent.extra.update_application_message_text",
String.valueOf(count == 0 ? "" : count));
context.sendBroadcast(localIntent);
}
}
private static void sendToSony(Context context, int count) {
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", LAUNCHER_ACTIVITY_NAME);//启动页
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);
}
private static void sendToSamsumg(Context context, int count) {
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", LAUNCHER_ACTIVITY_NAME);
context.sendBroadcast(intent);
}
private static String getLauncherClassName(Context context) {
PackageManager pm = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfos) {
String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
if (pkgName.equalsIgnoreCase(context.getPackageName())) {
String className = resolveInfo.activityInfo.name;
return className;
}
}
return null;
}
private static boolean isMIUI() {
String code = getSystemProperty("ro.miui.ui.version.code");
String name = getSystemProperty("ro.miui.ui.version.name");
return !TextUtils.isEmpty(code) && !TextUtils.isEmpty(name);
}
public static String getSystemProperty(String propName) {
String line;
BufferedReader input = null;
try {
Process p = Runtime.getRuntime().exec("getprop " + propName);
input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
line = input.readLine();
input.close();
} catch (IOException ex) {
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
// e.printStackTrace();
}
}
}
return line;
}
}
其中 LAUNCHER_ACTIVITY_NAME 是启动页,也可以用 getLauncherClassName()方法获取。
小米的MIUI6.0以上版本,必须使用通知栏的方式实现,而6.0之前是发广播的形式。isMIUI()方法判断了所有的MIUI系统,包括刷机的。
Sony没有测试机,未测试。小米三星测试有效
桌面图标未读消息(小米,sony,三星手机)的更多相关文章
- Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒
http://www.51itong.net/android-badgenumber-9789.html Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒 ...
- 【Python学习笔记】-APP图标显示未读消息数目
以小米手机系统为例,当安装的某个APP有未读消息时,就会在该APP图标的右上角显示未读消息的数目.本文主要解说怎样用Python语言实现图标显示未读消息的数目.首先,还是要用到Python中PIL库, ...
- [iOS微博项目 - 3.6] - 获取未读消息
github: https://github.com/hellovoidworld/HVWWeibo A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...
- wing带你玩转自定义view系列(2) 简单模仿qq未读消息去除效果
上一篇介绍了贝塞尔曲线的简单应用 仿360内存清理效果 这一篇带来一个 两条贝塞尔曲线的应用 : 仿qq未读消息去除效果. 转载请注明出处:http://blog.csdn.net/wingicho ...
- iOS 未读消息角标 仿QQ拖拽 简单灵活 支持xib(源码)
一.效果 二.简单用法 超级简单,2行代码集成:xib可0代码集成,只需拖一个view关联LFBadge类即可 //一般view上加角标 _badge1 = [[LFBadge alloc] init ...
- Android BGABadgeView:新消息/未接来电/未读消息/新通知圆球红点提示(1)
Android BGABadgeView:新消息/未接来电/未读消息/新通知圆球红点提示(1) 现在很多的APP会有新消息/未接来电/未读消息/新通知圆球红点提示,典型的以微信.QQ新消息提示为 ...
- Android 高仿QQ滑动弹出菜单标记已读、未读消息
在上一篇博客<Android 高仿微信(QQ)滑动弹出编辑.删除菜单效果,增加下拉刷新功能>里,已经带着大家学习如何使用SwipeMenuListView这一开源库实现滑动列表弹出菜单,接 ...
- 未读消息(小红点),前端与 RabbitMQ实时消息推送实践,贼简单~
前几天粉丝群里有个小伙伴问过:web 页面的未读消息(小红点)怎么实现比较简单,刚好本周手头有类似的开发任务,索性就整理出来供小伙伴们参考,没准哪天就能用得上呢. 之前在 <springboot ...
- android 实现类似qq未读消息点击循环显示
public void jumpUnread(boolean cycle) { List<ContactLogModel> dataList = adapter.getContactLog ...
随机推荐
- 使用LevelListDrawable实现Html.fromHtml多张图片显示
stackoverflow网站果然强大,帮了我不少忙! http://stackoverflow.com/questions/16179285/html-imagegetter-textview 首先 ...
- python的运行机制和版本区别
引用来自:here 解释型语言和编译型 首先,我们编程都是用的高级语言(写汇编和机器语言的大牛们除外),计算机不能直接理解高级语言,只能理解和运行机器语言,所以必须要把高级语言翻译成机器语言,计算机才 ...
- FileInputFormat看这一段源码
这是FileInputFormat中的一个方法,看一下它的功能,多看源码,理解hadoop,同时提高自己的java编程能力: private static String[] getPathString ...
- MySQL 之【视图】【触发器】【存储过程】【函数】【事物】【数据库锁】【数据库备份】
1.视图 视图:是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据 视图有如下特点; 1. 视图的列可以来自不同的表,是表的抽象和逻辑意义上建立的新关系. 2. 视 ...
- 使用 tabindex 改变Tab 键顺序
使用 tabindex原文 https://developers.google.cn/web/fundamentals/accessibility/focus/using-tabindex 在表单上使 ...
- weblogic启动项目,设置内容、设置的数据源链接不生效
昨天坑自己了一把,把weblogic的数据库连接方式由jdbc改成了jndi,然后不生效,还是走之前jdbc的连接地址. 因为数据库用户之前权限有问题,所以一直纠结于这个地方,忘记了缓存的原因. 后来 ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [替罪羊树]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- HTML5:表单提交
不加CSS.JavaScrips的HTML表单提交简单代码 <!DOCTYPE html> <html lang="en"> <head> &l ...
- iOS开发尺寸记录
https://kapeli.com/cheat_sheets/iOS_Design.docset/Contents/Resources/Documents/index https://help.ap ...
- angularjs 在 iframe 里面无法正确显示 src 指定的内容
原 controller : $scope.myURL = URL; 页面: <iframe ng-src='{{myURL}}' class="width-100 height-10 ...