新消息来了,在桌面的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. 十二、K3 WISE 开发插件《工业单据老单与自己添加的窗体 - 互相传值传参》

    ===================================== 目录: 1.演示效果--[销售订单]传值给[自定义窗体] 2.演示效果--[自定义窗体]传值给[销售订单] 3.附源码 4. ...

  2. 在浏览器中高效使用JavaScript module(模块)

    在浏览器中也可以使用JavaScript modules(模块功能)了.目前支持这一特性的浏览器包括: Safari 10.1. 谷歌浏览器(Canary 60) – 需要在chrome:flags里 ...

  3. angular ng-repeat中DOM的重新渲染机制(项目中遇到问题)

    昨天在项目中遇到一个问题:文本框中输入值只要变化就要请求接口获取数据赋值给couponData,这样性能不好.所以和服务器端沟通改成了服务器端一次性返回所有数据,这样前端只要获取一次数据然后保存在对象 ...

  4. WordOperate

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  5. 混合模式为什么成为占有率最高的app开发技术

    在企业移动战略布局中,app已成为连接业务与用户最主要的载体,同样其开发技术目前也处于十分成熟的阶段,而从技术实现的角度去考量,很多从业者可能并不知道,越是大企业.越是IT预算多的企业,他们的移动ap ...

  6. vue axios跨域请求,代理设置

    在config下的index.js中的dev下的 proxyTable{}中设置代理 proxyTable: { '/api': { target: 'http://10.0.100.7:8081', ...

  7. ML.NET教程之客户细分(聚类问题)

    理解问题 客户细分需要解决的问题是按照客户之间的相似特征区分不同客户群体.这个问题的先决条件中没有可供使用的客户分类列表,只有客户的人物画像. 数据集 已有的数据是公司的历史商业活动记录以及客户的购买 ...

  8. java虚拟机学习

    //20181129 ·Java虚拟机的内存分为三个部分:栈stack.堆heap.方法区method area----包含在“堆”里面,因为作用特殊所以单独列出来 ·栈的特点:     栈描述的是方 ...

  9. hive on tez配置

    1.Tez简介 Tez是Hontonworks开源的支持DAG作业的计算框架,它可以将多个有依赖的作业转换为一个作业从而大幅提升MapReduce作业的性能.Tez并不直接面向最终用户--事实上它允许 ...

  10. 9 tips to improve spoken english

    https://www.youtube.com/watch?v=FUW_FN8uzy0 1. don't translate everything into your native language ...