Android 如何在 Sony Xperia 设备上向应用程序图标添加通知标记/计数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20216806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a notification badge/count to application icon on Sony Xperia devices?
提问by Marcus
With Sony's Xperia Home, certain apps have the ability to display a count bubble or badge on the app icon. Facebook and Facebook Messenger both do this, as well as the built in Email app.
使用 Sony 的 Xperia Home,某些应用程序能够在应用程序图标上显示计数气泡或徽章。Facebook 和 Facebook Messenger 以及内置的电子邮件应用程序都可以这样做。
This has been solved for Samsung's launcher, but I have not come across any documentation on how to do it for Sony's launcher.
三星的启动器已经解决了这个问题,但我没有遇到任何关于如何为索尼的启动器执行此操作的文档。
How can it be done?
怎么做到呢?
回答by Marcus
After having seen Daniel Ochoa's solution for Samsung's launcher, which uses a BadgeProvider to handle the badges, I set out to do the same for Sony's Xperia Home. This answer is taken directly from my blog.
在看过 Daniel Ochoa 为三星发射器提供的解决方案后,它使用 BadgeProvider 来处理徽章,我开始为索尼的 Xperia Home 做同样的事情。这个答案直接取自我的博客。
How I figured it out - For anyone interested
我是怎么想出来的 - 对于任何有兴趣的人
I stumbled upon Sony's AppXploreand used it to check out the permission's of the Facebook app. The Facebook app requests the following permission, which is the key to displaying badges on Sony devices:
我偶然发现了索尼的 AppXplore并用它来检查 Facebook 应用程序的权限。Facebook 应用程序请求以下权限,这是在 Sony 设备上显示徽章的关键:
com.sonyericsson.home.permission.BROADCAST_BADGE
com.sonyericsson.home.permission.BROADCAST_BADGE
Next, I had a look through all available content providersbut I found nothing related to app icon badges there. I ran the command in this answerto get a system dump file and searched for "badge" using Notepad++. I found this:
接下来,我查看了所有可用的内容提供商,但在那里我没有发现任何与应用图标徽章相关的内容。我运行此答案中的命令以获取系统转储文件并使用 Notepad++ 搜索“徽章”。我找到了这个:
com.sonyericsson.home.action.UPDATE_BADGE: 41be9a90 com.sonyericsson.home/.BadgeService$BadgeReceiver filter 41be9858
com.sonyericsson.home.action.UPDATE_BADGE:41be9a90 com.sonyericsson.home/.BadgeService$BadgeReceiver 过滤器 41be9858
So, it's handled using a BroadcastReciever on Sony as opposed to Samsung's Content Provider. So, I created a dummy BroadcastReciever of my own, listening for the action com.sonyericsson.home.action.UPDATE_BADGE, and found the extras passed to Sony's BadgeService. For this, I also needed a permission, but that was easy to find in the dump file:
因此,它是使用 Sony 上的 BroadcastReciever 处理的,而不是三星的 Content Provider。因此,我创建了一个我自己的虚拟 BroadcastReciever,监听 com.sonyericsson.home.action.UPDATE_BADGE 的动作,并发现传递给 Sony 的 BadgeService 的额外内容。为此,我还需要一个权限,但在转储文件中很容易找到:
com.sonyericsson.home.permission.RECEIVE_BADGE
com.sonyericsson.home.permission.RECEIVE_BADGE
The extras sent by Facebook, the Email app, etc, are:
Facebook、电子邮件应用程序等发送的额外内容是:
- com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME- The name of your app's main activity, android.intent.action.MAIN. This is so the launcher knows which icon to show the badge on.
- com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE- a boolean indicating if we want to show the badge or not (which we do!)
- com.sonyericsson.home.intent.extra.badge.MESSAGE- a string (not an integer - that took me a while to realize...) with the number to show.
- com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME- The name of your application package.
- com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME- 应用的主要活动的名称,android.intent.action.MAIN。这样启动器就知道在哪个图标上显示徽章。
- com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE- 一个布尔值,指示我们是否要显示徽章(我们这样做了!)
- com.sonyericsson.home.intent.extra.badge.MESSAGE- 一个字符串(不是整数 - 我花了一段时间才意识到......)并显示数字。
- com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME- 您的应用程序包的名称。
How to show badges on your app's launcher icon on Sony Xperia devices
如何在 Sony Xperia 设备上的应用程序启动器图标上显示徽章
So, it turns out it's very simple to show a badge on your application icon in the launcher. IMO it's much more straight-forward than for Samsung's launcher. Here's a step-by-step-guide (and it's not long!)
因此,事实证明在启动器中的应用程序图标上显示徽章非常简单。IMO 比三星的发射器更直接。这是分步指南(而且时间不长!)
Declare the
com.sonyericsson.home.permission.BROADCAST_BADGE
permission in your manifest file:Broadcast an
Intent
to theBadgeReceiver
: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);
Done. Once this
Intent
is broadcast the launcher should show a badge on your application icon.To remove the badge again, simply send a new broadcast, this time with
SHOW_MESSAGE
set to false:intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
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);
完毕。一旦
Intent
广播,启动器应该在您的应用程序图标上显示一个徽章。要再次删除徽章,只需发送一个新的广播,这次
SHOW_MESSAGE
设置为 false:intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
Good to know
很高兴知道
The message is a string!
消息是一个字符串!
Since MESSAGE
is a String
, you can actually add words to the badge:
由于MESSAGE
是 a String
,您实际上可以在徽章中添加单词:
intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "Testing");
But I wouldn't do that 'cause it just looks weird.
但我不会那样做,因为它看起来很奇怪。
You have access to all apps!
您可以访问所有应用程序!
The BROADCAST_BADGE
permission does not only give you access to your own app's icon, but to ALL of them. For example, here's how you can set Facebook's badge:
该BROADCAST_BADGE
权限不仅可以让您访问自己的应用程序图标,还可以访问所有图标。例如,以下是设置 Facebook 徽章的方法:
Intent intent = new Intent();
intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.facebook.katana.LoginActivity");
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.facebook.katana");
sendBroadcast(intent);
I hope this has been of help to someone! :)
我希望这对某人有所帮助!:)
回答by Tadas Valaitis
I use this class for Samsung, Sony and HTC devices (also available https://gist.github.com/Tadas44/cdae2f5995f21bf1c27f). Don't forget to add <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />
to AndroidManifest.xml
我将此类用于三星、索尼和 HTC 设备(也可用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;
}
}
回答by Martin Lindstr?m
I realize that this question is quite old, but for historical purposes, the API for 3rd party applications to interact with the Xperia Home API for this particular feature was made public last year:
我意识到这个问题已经很老了,但出于历史目的,去年公开了用于与此特定功能的 Xperia Home API 交互的第 3 方应用程序的 API:
Xperia Home badge API now publicly available
With sample code here:
使用示例代码:
There is also a 3rd party library which supports most major phone vendors, including the Xperia Home API:
还有一个支持大多数主要手机供应商的 3rd 方库,包括 Xperia Home API:
回答by Manitoba
Well, this is pretty difficult to do. The only way I've found so far is to create a widget
that will handle both the app icon and the badge.
嗯,这很难做到。到目前为止,我发现的唯一方法是创建一个widget
可以同时处理应用程序图标和徽章的方法。
I highly suggest you to visit this page where you'll learn how to achieve that: Android: Is it possible to update a ImageView/ImageButton with a number to show the number of new messages?
我强烈建议您访问此页面,您将在其中了解如何实现该目标: Android:是否可以使用数字更新 ImageView/ImageButton 以显示新消息的数量?