首先,您需要将以下权限添加到您的 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