Android 桌面角標的適配

heinika1476775901521發表於2017-12-14

一個本不該存在在 android 系統中的功能。

為什麼這麼說呢?google從來沒有在android系統中設計這個功能。本人也非常不喜歡這麼做。。。 原本就有通知欄提醒的情況下,何必重複新增一個提醒。尤其對強迫症患者來說,一些無關緊要的提醒
卻讓桌面圖示上多了一個數字,而且根本不知道這個數字想告訴你什麼,簡直不能忍。直接看頂部的通
知多好啊!
既然一些(好吧,大部分)廠家,非要效仿ios,那肯定會有使用者提出這種需求了。但是要怎麼做呢?
google的文件上坑定是沒有了。只能去各個公司的開發著平臺上找了。。。
Ps: 今年的開發者大會上還有人問了這個問題。回答是:android一直都在變,他也不知道將來會是什
麼樣子。

ShortcutBadger:

support-devices

先推薦一個 GitHub Library: ShortcutBadger 如上圖,它整合了很多 launchers 的角標提醒。
但是很不幸的是,圖很美。現實並不是,所以如果想粗略的實現該功能,推薦使用上庫。

不得不說,整理這個的人還挺多的,又搜到一個:https://github.com/l123456789jy/Lazy/blob/master/lazylibrary/src/main/java/com/github/lazylibrary/util/BadgeUtil.java

這些都差不多,看著用吧。

不支援的廠商

魅族,中興,酷派...
必須點贊啊,不跟風。

華為

華為的文件很好找,說明也很清楚,還有Demo,真是服務周到。 文件地址:http://developer.huawei.com/consumer/cn/wiki/index.php?title=%E5%8D%8E%E4%B8%BA%E6%A1%8C%E9%9D%A2%E8%A7%92%E6%A0%87SDK%E4%B8%8B%E8%BD%BD 使用方式 manifest 中新增許可權。

<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE"/>
<uses-permission android:name="com.huawei.permission.sec.MDM"/>
複製程式碼

在程式碼中新增方法:

//檢測EMUI版本是否支援
public void checkIsSupportedByVersion(){
    try {
      PackageManager manager = getPackageManager();
      PackageInfo info = manager.getPackageInfo("com.huawei.android.launcher", 0);
      if(info.versionCode>=63029){
        isSupportedBade = true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

//控制顯示的個數
private void handleBadge(int num){
  if(!isSupportedBade){
    Log.i("badgedemo", "not supported badge!");
    return;
  }
  try{
    Bundle bunlde =new Bundle();
    bunlde.putString("package", "com.example.badgedemo");
    bunlde.putString("class", "com.example.badgedemo.MainActivity");
    bunlde.putInt("badgenumber",num);
    ContentResolver t=this.getContentResolver();
    Bundle result=t.call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_launcher_badge", "", bunlde);
  }catch(Exception e){
    e.printStackTrace();
  }
}
複製程式碼

小米

文件地址
小米還算良心,文件比較好找。

基本介紹

預設的情況
當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

金立

網上根本找不到相關文件。

以上三家公司的開發者平臺,結構幾乎一模一樣,真是佩服!

相關文章