新消息来了,在桌面的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,三星手机)的更多相关文章

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

    http://www.51itong.net/android-badgenumber-9789.html Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒 ...

  2. 【Python学习笔记】-APP图标显示未读消息数目

    以小米手机系统为例,当安装的某个APP有未读消息时,就会在该APP图标的右上角显示未读消息的数目.本文主要解说怎样用Python语言实现图标显示未读消息的数目.首先,还是要用到Python中PIL库, ...

  3. [iOS微博项目 - 3.6] - 获取未读消息

    github: https://github.com/hellovoidworld/HVWWeibo   A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...

  4. wing带你玩转自定义view系列(2) 简单模仿qq未读消息去除效果

    上一篇介绍了贝塞尔曲线的简单应用 仿360内存清理效果 这一篇带来一个  两条贝塞尔曲线的应用 : 仿qq未读消息去除效果. 转载请注明出处:http://blog.csdn.net/wingicho ...

  5. iOS 未读消息角标 仿QQ拖拽 简单灵活 支持xib(源码)

    一.效果 二.简单用法 超级简单,2行代码集成:xib可0代码集成,只需拖一个view关联LFBadge类即可 //一般view上加角标 _badge1 = [[LFBadge alloc] init ...

  6. Android BGABadgeView:新消息/未接来电/未读消息/新通知圆球红点提示(1)

     Android BGABadgeView:新消息/未接来电/未读消息/新通知圆球红点提示(1) 现在很多的APP会有新消息/未接来电/未读消息/新通知圆球红点提示,典型的以微信.QQ新消息提示为 ...

  7. Android 高仿QQ滑动弹出菜单标记已读、未读消息

    在上一篇博客<Android 高仿微信(QQ)滑动弹出编辑.删除菜单效果,增加下拉刷新功能>里,已经带着大家学习如何使用SwipeMenuListView这一开源库实现滑动列表弹出菜单,接 ...

  8. 未读消息(小红点),前端与 RabbitMQ实时消息推送实践,贼简单~

    前几天粉丝群里有个小伙伴问过:web 页面的未读消息(小红点)怎么实现比较简单,刚好本周手头有类似的开发任务,索性就整理出来供小伙伴们参考,没准哪天就能用得上呢. 之前在 <springboot ...

  9. android 实现类似qq未读消息点击循环显示

    public void jumpUnread(boolean cycle) { List<ContactLogModel> dataList = adapter.getContactLog ...

随机推荐

  1. VS 2013 未找到与约束contractname Microsoft.VisualStudio.Utilities.IContentTypeRegistryService......匹配的导出

    错误信息: 点击解决方案中的文件,就会提示这个错误.最近给vs2013安装了python的插件,安装了vs2017的python,可能是这两个导致了这个错误. 解决方案: 删除 C:\Users\ji ...

  2. Office Web Apps Server

    Office Web Apps Server Office Web Apps Server 是一款 Office 服务器产品,可提供针对 Office 文件的基于浏览器的文件查看和编辑服务.Offic ...

  3. 错误:22 http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu bionic Release 404 Not Found [IP: 91.189.95.83 80]

    https://blog.csdn.net/chenbetter1996/article/details/80255552 到仓库地址找到哦该文件 删除两个文件就可以了

  4. ubuntu经常断网、掉线、上不去网的原因

    方案一: ubuntu经常断网.掉线.上不去网的原因,很可能是因为IPv6的关系,Ubuntu默认开启IPv6,在“设置--wifi--齿轮图标”中关掉就可以了. 经我环境测试,此方法无效 方案二: ...

  5. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

  6. Python生成器表达式

    https://www.cnblogs.com/liu-shuai/p/6098218.html 简介: 生成器表达式并不真正的创建数字列表,而是返回一个生成器对象,此对象在每次计算出一个条目后,把这 ...

  7. LeetCode 766 Toeplitz Matrix 解题报告

    题目要求 A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...

  8. linux crypt()函数使用总结

    原型: char *crypt(const char *key, const char *salt); 标准说明: crypt()算法会接受一个最长可达8字符的密钥(即key),并施以数据加密算法(D ...

  9. java 数组(二)

    public class ArrayDemo{ public static void main(String[] args){ int[] arr = {1,5,3,8,2,9,17,13}; get ...

  10. JavaScript、关于元素的offset~和client~

    1.偏移量(offset dimension)   =>offsetLeft(元素距离其父元素左边框(/上边框)的距离) =>offsetTop =>offsetWidth(元素的宽 ...