桌面图标未读消息(小米,sony,三星手机)

author author     2022-08-21     490

关键词:

新消息来了,在桌面的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没有测试机,未测试。小米三星测试有效

 

Android 未读消息计数显示在应用程序图标上

】Android未读消息计数显示在应用程序图标上【英文标题】:Androidunreadmessagecountdisplayonappicon【发布时间】:2014-04-1605:24:11【问题描述】:我想在android(如ios设备)的应用程序图标中添加未读消息计数更新。搜索后我才知道唯一... 查看详情

TideSDK - 显示未读计数(图标徽章)

】TideSDK-显示未读计数(图标徽章)【英文标题】:TideSDK-Showunreadcount(IconBadges)【发布时间】:2013-10-1512:54:29【问题描述】:我打算用TideSDK创建一个基本的聊天程序。我现在想知道是否可以在停靠/托盘图标中显示新的/未读的消... 查看详情

关于android类似qq和微信那种在桌面图标右上角更新数字的研究

...种情况 发通知栏消息的时候会自动加数字/***      * 三星手机:应用图标的快捷方式上加数字      * @param context      * @param num      */    public static void samsungShortCut(Context context, String num)     ... 查看详情

小米手机root后怎么恢复?

参考技术A1、首先,在桌面图标中找到“安全中心”,点击打开。2、打开“安全中心”以后,点击“立即优化”。3、然后找到“支付环境存在风险”这一项,点击打开。4、接着,我们点击“立即修复”即可。5、将手机连上网,... 查看详情

Twilio 可编程聊天 — 使应用程序徽章与未读消息计数保持同步

...间】:2017-12-1709:29:36【问题描述】:我们希望我们应用的图标徽章编号能够反映未读Twilio消息的总数。在应用程序中,我们使用委托方法来获取所有渠道中未读消息的计数,但是当应用程序处于后台时我们无法执行此操作。根 查看详情

如何在小米5手机上禁止app的消息推送?

...心这个APP软件会乱弹出垃圾信息了。参考技术A1.打开手机桌面,找到设置按钮并点击进入。2.打开设置,找到通知与状态栏选项。3.点击通知管理,会出现手机所有安装的所有APP及其推送信息通知状态。4.在通知管理里面找到你需... 查看详情

小米手机连接macbook

...2、选择下MAC系统安装驱动3、将小米手机连接电脑就会在桌面出现“XIAOMI”图标,这时候双击打开4、在出现的文件夹中,选择第一个文件打开就会出现一个进度条了5、将左边的安卓小人拖向右侧的文件夹中即可。6、点击图标就... 查看详情

小米手机怎么开启无障碍服务

...下方),双击点击风扇图标。2/3这时,进入了新的界面,桌面左下角有“设置”图标,双击设置图标,出现“锁屏画报界面”,这时点击手机的返回键,退回手机功能界面,这时候的手机不能够实现翻页,打开功能键,各个功能... 查看详情

MailCore2,只获取未读消息,或者知道哪些是未读消息

】MailCore2,只获取未读消息,或者知道哪些是未读消息【英文标题】:MailCore2,fetchonlyUNREADmessages,orknowwhichonesareUNREAD【发布时间】:2013-10-0422:44:21【问题描述】:我可以使用fetchMessagesByUIDOperationWithFolder:获取所有消息,但是,当一... 查看详情

如何收听消息阅读事件

...022:19:08【问题描述】:我正在制作一个小部件,其中我在图标上显示未读消息计数。我正在监听传入的消息,我可以使用意图过滤器增加计数:"android.provider.Telephony.SMS_RECEIVED"我还可以随时通过以下方式获取未读消息数:finalUriSM... 查看详情

socket,长连接,消息推送,消息提醒,未读消息提醒,消息通知,未读消息通知

今天公司要搞个类似QQ空间的未读消息通知,于是想到用WebSocket长连接,搜索一些资料后,调试好久,还以为不行,结果发现是IIS版本的原因,我本地用的Win7的系统,是IIS6的,本地测试一直不行,后来查资料发现用这个WebSock... 查看详情

微信图标不见了怎么办

参考技术A手机桌面微信图标不见了怎么办?解决手机图标消失的方法:1、手机“设置”进入。2、找到“应用程序”点击进入。3、选择全部,找到“华为桌面”程序点击进入。4、在“华为桌面”这个程序的信息中点击“清除数... 查看详情

Cassandra 未读消息计数

】Cassandra未读消息计数【英文标题】:Cassandraunreadmessagescount【发布时间】:2018-08-2805:01:12【问题描述】:请尝试模拟未读消息计数。我有一个user_messages表创建表user_messages(用户文本,发件人文字,消息文本,读取布尔值,创建... 查看详情

小米root权限怎么开启

参考技术A  1、首先我们解锁手机屏幕,在手机桌面上左右的滑动,找到小米手机的“设置”图标。  2、接着在打开的小米手机的设置页面中,我们点击“授权管理”的菜单项。  3、然后在打开的小米手机的授权管理页面中,... 查看详情

未读消息总数 quickblox

】未读消息总数quickblox【英文标题】:Totalunreadmessagecountquickblox【发布时间】:2015-07-1201:48:04【问题描述】:你好如何获取quicblox中未读消息的总数?对于所有对话框。有没有直接的api调用?谢谢!【问题讨论】:您应该首先介... 查看详情

小米手机上的setAlarmClock()不显示闹钟图标指示

】小米手机上的setAlarmClock()不显示闹钟图标指示【英文标题】:setAlarmClock()onXiaomiphonedoesnotdisplaythealarmclockiconindicator【发布时间】:2021-07-0919:29:51【问题描述】:在我的安卓应用程序中,我使用setAlarmClock()函数来安排闹钟。正如a... 查看详情

FB PHP SDK 将未读消息状态更改为已读和计数未读消息

】FBPHPSDK将未读消息状态更改为已读和计数未读消息【英文标题】:FBPHPSDKchangeunreadmessagestatustoreadandcountingUnreadMessages【发布时间】:2015-02-2414:46:39【问题描述】:在找不到解决方案后,我需要一些帮助。有类似的问题here,但没... 查看详情

仅从 QuickBlox 获取未读消息?

】仅从QuickBlox获取未读消息?【英文标题】:GetOnlyUnreadMessagesfromQuickBlox?【发布时间】:2015-01-3004:59:22【问题描述】:我在我的聊天应用程序中集成了QuickBloxSDK。我对未读消息或离线消息问题感到有点沮丧。在离线消息QuickBlox中... 查看详情