37

三星的 TWLauncher 允许应用程序在应用程序图标上创建徽章计数。

这是完全无证的!任何地方都没有提到它,只有少数应用程序在使用它(例如 Facebook、eBay)。

您如何使用此功能为您的应用程序图标添加计数?

这是非常特定于三星设备的。我不是在问一般的Android。我只是在问三星的 Touchwhiz 界面,它目前允许标记。安卓没有。

4

7 回答 7

69

首先,您需要将以下权限添加到您的 AndroidManifest.xml 文件中。

<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />

列结构如下:

(integer) _id, (text) package, (text) class, (integer) badgecount, (blob) icon, (???) extraData

为了从 BadgeProvider 查询所有结果,请执行以下操作:

// This is the content uri for the BadgeProvider
Uri uri = Uri.parse("content://com.sec.badge/apps");

Cursor c = getContentResolver().query(uri, null, null, null, null);

// This indicates the provider doesn't exist and you probably aren't running
// on a Samsung phone running TWLauncher. This has to be outside of try/finally block
if (c == null) {
    return;
}

try {
    if (!c.moveToFirst()) {
        // No results. Nothing to query
        return;
    }

    c.moveToPosition(-1);
    while (c.moveToNext()) {
        String pkg = c.getString(1);
        String clazz = c.getString(2);
        int badgeCount = c.getInt(3);
        Log.d("BadgeTest", "package: " + pkg + ", class: " + clazz + ", count: " + String.valueOf(cnt));
    }
} finally {
    c.close();
}

为了将徽章计数添加到您的应用程序图标

ContentValues cv = new ContentValues();
cv.put("package", getPackageName());
// Name of your activity declared in the manifest as android.intent.action.MAIN.
// Must be fully qualified name as shown below
cv.put("class", "com.example.badge.activity.Test");
cv.put("badgecount", 1); // integer count you want to display

// Execute insert
getContentResolver().insert(Uri.parse("content://com.sec.badge/apps"), cv);

如果您想清除图标上的徽章计数

ContentValues cv = new ContentValues();
cv.put("badgecount", 0);
getContentResolver().update(Uri.parse("content://com.sec.badge/apps"), cv, "package=?", new String[] {getPackageName()});  


我创建了一个开源项目,您可以将其作为库导入以协助完成此操作。它已作为 Apache 获得许可,因此请随意使用它。

你可以从这里得到它:https ://github.com/shafty023/SamsungBadger

于 2013-11-22T03:25:50.923 回答
21

还有另一个很酷的开源库,支持不同的设备: https ://github.com/leolin310148/ShortcutBadger/

于 2014-07-17T13:32:01.140 回答
17

将这些权限添加到清单

<!--for android badge-->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS"/>

<!--for Samsung badge-->
<uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>

<!--for htc badge-->
<uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT"/>

<!--for sony badge-->
<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"/>

<!--for apex badge-->
<uses-permission android:name="com.anddoes.launcher.permission.UPDATE_COUNT"/>

将这些包名称添加到您的课程中:

    final String HOME_PACKAGE_SONY = "com.sonyericsson.home";
    final String HOME_PACKAGE_SAMSUNG = "com.sec.android.app.launcher";
    final String HOME_PACKAGE_LG = "com.lge.launcher2";
    final String HOME_PACKAGE_HTC = "com.htc.launcher";
    final String HOME_PACKAGE_ANDROID = "com.android.launcher";
    final String HOME_PACKAGE_APEX = "com.anddoes.launcher";
    final String HOME_PACKAGE_ADW = "org.adw.launcher";
    final String HOME_PACKAGE_ADW_EX = "org.adwfreak.launcher";
    final String HOME_PACKAGE_NOVA = "com.teslacoilsw.launcher";

用来 :

  // just put your pachage and main activity class path
  String classPath = "ir.faasaa.resa.MainActivity";

           ContentValues cv = new ContentValues();
                    cv.put("package", context.getPackageName());
                    cv.put("class", classPath);
                    cv.put("badgecount", count);
           context.getContentResolver().insert(Uri.parse(HOME_PACKAGE_SAMSUNG), cv);

感谢ShortcutBadger

于 2015-02-15T06:05:23.773 回答
8

在 android 中,我们没有 iOS 那样的徽章样式方法,但一些制造商支持在应用程序图标上显示徽章。

支持索尼和HTC。

android中应用程序上的徽章样式图标示例

Sony 和 HTC 支持在应用图标上添加徽章。

对于索尼

<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />  

Intent intent= new Intent("com.sonyericsson.home.action.UPDATE_BADGE");

intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", Class Name);

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

intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE",number);

intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", packageName);

sendBroadcast(intent);

宏达电:

<uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS" />  
 <uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT" />  

Intent updateIntent = new Intent("com.htc.launcher.action.UPDATE_SHORTCUT");
updateIntent.putExtra("packagename", packageName);
updateIntent.putExtra("count", number);
this.sendBroadcast(updateIntent);

Intent setNotificationIntent = new Intent("com.htc.launcher.action.SET_NOTIFICATION");
ComponentName localComponentName = new ComponentName(packageName, className);
setNotificationIntent.putExtra("com.htc.launcher.extra.COMPONENT", localComponentName.flattenToShortString());
setNotificationIntent.putExtra("com.htc.launcher.extra.COUNT", number);
this.sendBroadcast(setNotificationIntent);
于 2014-07-14T10:53:39.370 回答
5

我将此类用于三星和索尼设备(也可使用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;
    }
}
于 2014-09-15T13:30:48.130 回答
0

还有另一种工具可用于 Xperia 设备

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.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "10");

更多详情如下

http://www.marcusforsberg.net/blog/android-notification-badge-app-icon-sony/

于 2014-09-09T11:36:56.823 回答
0

对于 Android 8.0 Oreo 用户-

对于那些在显示带有 Oreo 的三星 S8、S9 或 Pixel设备的徽章计数时遇到问题的人,请注意ShortcutBadger库不支持在问题API>26Samsung报告的此处。如果是这样,您必须先检查它-isBadgeCounterSupported

public boolean isBadgeCounterSupported() {
    // Workaround for bug in ShortcutBadger in version 1.1.19, registered as
    // https://github.com/leolin310148/ShortcutBadger/issues/266
    if (Build.MANUFACTURER.equalsIgnoreCase("Samsung") && Build.VERSION.SDK_INT >= 26) {
        Log.d("LOG_TAG", "Launcher icon badge (ShortcutBadger) is not supported on Samsung devices running Android 8 (or newer).");
        return false;
    }

    return ShortcutBadger.isBadgeCounterSupported(context);
}

唯一的方法是从Notification应用Badges。至于从通知中应用徽章,请参阅Android 文档,并注意“设置自定义通知计数”部分。

Notification notification = new NotificationCompat.Builder(MainActivity.this, "my_channel_01")
    .setContentTitle("New Messages")
    .setContentText("You've received 3 new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
    .setNumber(messageCount) //this badge count
    .build();

在此处输入图像描述

有关 Android 8.0 中通知的更多信息,请参阅此答案

于 2018-05-23T08:18:14.850 回答