新消息来了,在桌面的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. 跨服务器做yum源

    服务器无法上网,然后自己根据光盘搭建的YUM源不够用.RPM安装软件,各种依赖,找包烦死. 先做个 能上外网的 http proxy 找一个可以上Internet的服务器,然后起一个squid服务, ...

  2. TP5和TP3.2的区别

    1.控制器输出 return $this->fetch("index/hello"); $this->display 单字母函数去掉了 如:M() D() U() S( ...

  3. Linux 更新vim

    https://blog.csdn.net/linuxnews/article/details/52938583 https://blog.csdn.net/nzyalj/article/detail ...

  4. postgresql----聚合函数

    聚合函数是从一组输入中计算出一个结果的函数. 测试表 test=# \d tbl_test Table "public.tbl_test" Column | Type | Modi ...

  5. 用U盘制作并安装WIN10 64位原版系统的详细教程(该方法应该适用于任何一版的原版操作系统)

    https://www.cnblogs.com/Jerseyblog/p/6518273.html

  6. notbook1.1

    1)95年创建java语言 2)1.2版本使java具有了生产能力 3)1.8版本增加 新的API lamda 函数式编程1.9 使之模块化 4)java编程的基本套路 1. 编写源代码2. 编译源程 ...

  7. React兄弟、父子元素之间的通信

    React兄弟.父子元素之间的通信 React元素之间的通信主要由下面几种方式 1. Redux 2. EventEmitter 3. 通过props进行通信(需要有嵌套关系) 子元素到父元素 父子元 ...

  8. 简单示例用例(Simple Example Use Cases)--hive GettingStarted用例翻译

    1.MovieLens User Ratings First, create a table with tab-delimited text file format: 首先,创建一个通过tab分隔的表 ...

  9. [skill][https][ssl/tls] HTTPS相关知识汇总

    结论前置: A 身份验证 证书, 服务器证书 B 密钥协商 RSA   DHE / ECDHE   PSK C 加密通信 加密通信采用对称加密,使用B阶段协商出来的密钥. B 阶段如果使用 RSA 协 ...

  10. mysql设置指定ip访问,用户权限相关操作

    基础语法GRANT priv_type ON database.table TO user[IDENTIFIED BY [PASSWORD] 'password'] [,user [IDENTIFIE ...