【问题标题】:adding notification badge on app icon in android在android中的应用程序图标上添加通知徽章
【发布时间】:2013-10-05 20:12:52
【问题描述】:

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

首先它说就像 facebook 有三星设备的通知徽章一样。 How does Facebook add badge numbers on app icon in Android?。原来是touchwiz启动器,它可能是三星设备的默认启动器。当我尝试使用 nova 启动器测试我的三星设备时。它需要安装一些第三方软件或 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 中应用程序图标中的徽章。 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?

所以我正在寻找与供应商特定的启动器交互的解决方案。

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

谢谢

【问题讨论】:

标签: android user-interface icons badge


【解决方案1】:

我将这个类用于三星和索尼设备(也可用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;
    }
}

【讨论】:

  • 在我的情况下它适用于三星设备,但现在看来它只适用于三星和索尼设备。因此,我们需要更新此答案,使其也适用于其他制造商。
【解决方案2】:

除了 Daniel Ochoa 为三星提供的解决方案(参见关于 OP 的评论)之外,我还弄清楚了索尼设备是如何做到这一点的。

我已经在博客上写了 here。我还发布了一个关于此here 的单独 SO 问题。


Sony 设备使用名为 BadgeReciever 的类。

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

  2. 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);
    
  3. 完成。广播此Intent 后,启动器应在您的应用程序图标上显示一个徽章。

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

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

我已排除有关如何找到此问题的详细信息以保持答案简短,但所有内容都可以在博客中找到。对某人来说可能是一本有趣的书。

【讨论】:

  • 由于这个问题没有答案而且你没有代表,我不会标记这个(我已经得到你的答案来审查)。不过,我建议您使用这些链接中的一些相关信息来填补这个问题。我已对您的其他问题投了赞成票,因此您可以回答。
  • 感谢@OGHaza。我已经在这个答案中添加了一些代码,现在将回答我的主要问题。
猜你喜欢
  • 2015-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 2021-08-26
  • 1970-01-01
  • 2022-08-07
  • 1970-01-01
相关资源
最近更新 更多