一個本不該存在在 android 系統中的功能。
為什么這么說呢?google從來沒有在android系統中設計這個功能。本人也非常不喜歡這么做。。。
原本就有通知欄提醒的情況下,何必重復添加一個提醒。尤其對強迫癥患者來說,一些無關緊要的提醒
卻讓桌面圖標上多了一個數字,而且根本不知道這個數字想告訴你什么,簡直不能忍。直接看頂部的通
知多好啊!
既然一些(好吧,大部分)廠家,非要效仿ios,那肯定會有用戶提出這種需求了。但是要怎么做呢?
google的文檔上坑定是沒有了。只能去各個公司的開發著平臺上找了。。。
Ps: 今年的開發者大會上還有人問了這個問題。回答是:android一直都在變,他也不知道將來會是什
么樣子。
ShortcutBadger:
先推薦一個 GitHub Library: ShortcutBadger
如上圖,它集成了很多 launchers 的角標提醒。
但是很不幸的是,圖很美。現實并不是,所以如果想粗略的實現該功能,推薦使用上庫。
不得不說,整理這個的人還挺多的,又搜到一個:https://github.com/l123456789jy/Lazy/blob/master/lazylibrary/src/main/java/com/github/lazylibrary/util/BadgeUtil.java
這些都差不多,看著用吧。
不支持的廠商https://developer.huawei.com/consumer/cn/doc/30802
魅族,中興,酷派...
必須點贊啊,不跟風。
華為
華為的文檔很好找,說明也很清楚,還有Demo,真是服務周到。
https://developer.huawei.com/consumer/cn/doc/30802
小米
文檔地址
小米還算良心,文檔比較好找。
基本介紹
默認的情況
當app 向通知欄發送了一條通知 (通知不帶進度條并且用戶可以刪除的),那么桌面app icon角標就會顯示1.
此時app顯示的角標數是和通知欄里app發送的通知數對應的,即向通知欄發送了多少通知就會顯示多少角標。
通知可以定義角標數
例如 有5封未讀郵件,通知欄里只會顯示一條通知,但是想讓角標顯示5. 可以在發通知時加個標示。
實現代碼
第三方app需要用反射來調用,參考代碼:
NotificationManager mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this)
.setContentTitle(“title”).setContentText(“text”).setSmallIcon(R.drawable.icon);
Notification notification = builder.build();
try {
Field field = notification.getClass().getDeclaredField(“extraNotification”);
Object extraNotification = field.get(notification);
Method method = extraNotification.getClass().getDeclaredMethod(“setMessageCount”, int.class);
method.invoke(extraNotification, mCount);
} catch (Exception e) {
e.printStackTrace();
}
mNotificationManager.notify(0,notification);
三星
官方只找到了應用商店上傳應用的一些文檔和主題文檔
算了,高手在民間吧。
額,我又犯了一個錯誤。我先用的是google中文的搜索。。。
中文 | 英文 |
---|---|
chinese.png
|
english.png
|
先看看中文搜索結果
中文找到了:找到了簡書這篇Badge分析&如何逼死處女座
以下方法轉自那篇簡書文章:
方法一
通過三星Launcher自己的廣播,來給應用添加角標:
/**
* 設置三星的Badge
*
* @param context context
* @param count count
*/
private static void setBadgeOfSumsung(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);
}
此方法不需要任何權限,只需要知道App的包名和類名。因此,你當然可以在程序里面給其它任意一個App設置任意數量的角標,
而且沒有任何提示,是的,很流氓,誰說不是呢,當然別說是我告訴你的,你就所你是百度的。例如:
intent.putExtra("badge_count_package_name", "com.tencent.mobileqq");
intent.putExtra("badge_count_class_name", "com.tencent.mobileqq.activity.SplashActivity");
將包名和類名用QQ的替換下,然后你就可以隨心所欲、為所欲為了。
方法二
https://github.com/shafty023/SamsungBadger
三年前更新的,不知道還能用不。。。
再看看英文吧
點開第一個stackoverflow
點贊最多的答案:
First you'll need to add the following permissions to your AndroidManifest.xml file.
<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />
The column structure is as follows:
(integer) _id, (text) package, (text) class, (integer) badgecount, (blob) icon, (???) extraData
In order to query ALL results from the BadgeProvider do the following:
// 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();
}
In order to add a badge count to your application icon
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);
If you want to clear the badge count on your icon
ContentValues cv = new ContentValues();
cv.put("badgecount", 0);
getContentResolver().update(Uri.parse("content://com.sec.badge/apps"), cv, "package=?", new String[] {getPackageName()});
NEW
I have created an open source project that you can import as a library to assist with this. It's licensed as Apache so feel free to use it as you please.
You can get it from here: https://github.com/shafty023/SamsungBadger
額,這不就是那個三年前停止更新的github的項目嗎。。。。崩潰!
點贊第二多的:
There is another cool open source library that support different devices:
https://github.com/leolin310148/ShortcutBadger/
好吧,都是見過的。
LG
據說和 samsung 一樣。
sony
果然大廠 google 一下就找到了。
地址:https://developer.sony.com/2016/06/23/xperia-home-badge-api-now-publicly-available/
oppo
網上根本找不到相關文檔。
這種肆意模仿 apple 的公司自然有這個功能了。然而文檔并沒有,而且網上幾乎搜不到。。。
github:ShortcutBadger說有支持。沒有測試機,沒法測試啊。。。而且根本不知道他們什么時候開始支持的。
最近在百度云測上測了一下,github:ShortcutBadger 這個項目可以。
vivo
網上根本找不到相關文檔。
這篇說可以,不知道行不行。沒有測試機。
http://www.lai18.com/content/9532859.html
這一篇看起來比較靠譜:
http://blog.csdn.net/dbs1215/article/details/53054073
金立
網上根本找不到相關文檔。
以上三家公司的開發者平臺,結構幾乎一模一樣,真是佩服!