首页 文章

在android中的app图标上添加通知徽章

提问于
浏览
5

您可能认为这是一个重复的问题,但我希望答案比任何类似的问题更具体 .

首先它说像facebook有三星设备的通知徽章 . How does Facebook add badge numbers on app icon in Android? . 事实证明它是touchwiz Launcher ,它可能是三星设备的默认 Launcher . 当我试图用新星 Launcher 测试我的三星设备 . 它需要安装一些第三方软件或Nova TestlaUnread .

这是事实 . 我认为touchWiz正在为所有应用程序添加徽章,但是当我尝试使用我的应用程序进行测试以获得一些通知时,它不会显示任何徽章 . 因为我发现三星TouchWiz只显示应用程序到这里所述的选定应用程序:UI help for Notification on icon

The question is: is there any way to let know the touchWiz to add badge to my app-icon? 这是三星设备,

以前的论点,看起来不同供应商的启动器是负责Android中app图标的徽章 . Sony Experia使用Experia Home / Launcher作为其默认启动器 . 在我的索尼设备中,他们在那里有一些徽章或短信或Facebook .

The question is: like the previous question, is there any way to let know experia home to add badge to my app icon?

因为我认为可能与启动器交互似乎是“添加徽章到应用程序图标”的解决方案 .

并且有关于此活动别名的解决方案,并且不时更改应用程序图标,我认为这是垃圾 . Is there a way to add a badge to an application icon in Android?

所以我正在寻找与供应商特定的 Launcher 交互的解决方案 .

'任何解决方案都会受到赞赏,即使我会深入使用android中的原生开发'

谢谢你

2 回答

  • 0

    我将这个类用于三星和索尼设备(也可用https://gist.github.com/Tadas44/cdae2f5995f21bf1c27f) . 不要忘记将 <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /> 添加到AndroidManifest.xml

    public class BadgeUtils {
    
    
        public static void setBadge(Context context, int count) {
            setBadgeSamsung(context, count);
            setBadgeSony(context, count);
        }
    
        public static void clearBadge(Context context) {
            setBadgeSamsung(context, 0);
            clearBadgeSony(context);
        }
    
    
        private static void setBadgeSamsung(Context context, int count) {
            String launcherClassName = getLauncherClassName(context);
            if (launcherClassName == null) {
                return;
            }
            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", launcherClassName);
            context.sendBroadcast(intent);
        }
    
        private static void setBadgeSony(Context context, int count) {
            String launcherClassName = getLauncherClassName(context);
            if (launcherClassName == null) {
                return;
            }
    
            Intent intent = new Intent();
            intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
    
            context.sendBroadcast(intent);
        }
    
    
        private static void clearBadgeSony(Context context) {
            String launcherClassName = getLauncherClassName(context);
            if (launcherClassName == null) {
                return;
            }
    
            Intent intent = new Intent();
            intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(0));
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
    
            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;
        }
    }
    
  • 4

    除了Daniel Ochoa的三星解决方案(见OP评论)之外,我还想出了如何为索尼设备做到这一点 .

    我发表了关于它的博客here . 我还发布了一个关于这个here的单独的问题 .


    Sony设备使用名为 BadgeReciever 的类 .

    • 在清单文件中声明 com.sonyericsson.home.permission.BROADCAST_BADGE 权限:

    • Intent 广播到 BadgeReceiver

    Intent intent = new Intent();
    
    intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.yourdomain.yourapp.MainActivity");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "99");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", "com.yourdomain.yourapp");
    
    sendBroadcast(intent);
    
    • 完成 . 广播后,启动器应在应用程序图标上显示徽章 .

    • 要再次删除徽章,只需发送一个新广播,这次将 SHOW_MESSAGE 设置为false:

    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
    

    我已经排除了有关我如何找到这个以保持答案简短的细节,但这些都在博客中提供 . 对某人来说,这可能是一个有趣的读物 .

相关问题