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

时间:2013-09-30 03:47:35

标签: android user-interface icons badge

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

首先它说像facebook有三星设备的通知徽章。 How does Facebook add badge numbers on app icon in Android?。事实证明它是touchwiz发射器,它可能是三星设备的默认发射器。当我试图用新星发射器测试我的三星设备。它需要安装一些第三方软件或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?

所以我正在寻找与供应商特定的发射器进行交互的解决方案。

'任何解决方案都会受到赞赏,即使我会在android'

中使用本机开发

提前致谢

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

除了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);
    
  5. 我已经排除了有关我如何找到这个以保持答案简短的细节,但这些都可以在博客中找到。对某人来说,这可能是一本有趣的读物。