Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

不用自訂版面也能改通知列圖示:Android Notification 的迂迴解法

編輯:關於 Android 程式設計

在開發一個與通知列相關的功能時遇到一個限制:專案是插件化的架構,無法引入系統的布局資源檔,也就等於無法用標準的 RemoteViews 自訂版面,連通知列的 icon 都換不了。

網路上流傳一種不靠版面檔換 icon 的做法,但由於 Android 開放性帶來的碎片化——部分手機廠商會直接改動通知列的原始碼——這類偏門技巧的相容性相當有限。有興趣可以參考:https://blog.csdn.net/z1074971432/article/details/10446715

為了覆蓋大多數機型,這裡衍生出一種「曲線救國」的折衷方案:直接拿系統正在使用的那個通知版面,把它裡面的 ImageView 找出來替換掉。

核心思路

關鍵在於 Notification.contentView 這個欄位。它本來是我們用來塞自訂 RemoteViews 的地方,但如果反過來,先用一個空的 Notification 讓系統幫我們 apply 出預設版面,就能拿到一個真實的 View 樹,再逐一走訪其中的子檢視。

public void show(String title, CharSequence content, Bitmap bitmap, PendingIntent intent) {
    // 在 2.3 的機子上,若 id 相同的 notification 一個帶 ContentIntent 一個不帶,會拋異常
    _notification.setLatestEventInfo(_context, "", "", null);
    _notification.flags = _flag;

    RemoteViews hide = _notification.contentView;
    initView(_notification, hide, title, content, intent, bitmap);

    _manager.notify(_id, _notification);
}

public void cancel() {
    _manager.cancel(_id);
}

真正的重頭戲在 initView():

@SuppressWarnings("deprecation")
void initView(final Notification bar, final RemoteViews views, String title,
              CharSequence text, PendingIntent intent, final Bitmap bitmap) {
    bar.contentView = null;
    bar.setLatestEventInfo(_context, title, text, intent);

    // 以一個臨時 notification 讓系統 apply 出預設版面,取得真實的 View 樹
    Notification notification = new Notification();
    notification.setLatestEventInfo(_context, "", "", null);
    View view = notification.contentView.apply(_context, null);
    ViewGroup group = (ViewGroup) view;

    findView(group, new ViewVisitor() {
        @Override
        public void onFindView(View item) {
            if (item instanceof ImageView) {
                // 將原有 icon 隱藏:某些小米機型上系統 icon 會覆蓋我們的設定
                bar.contentView.setInt(item.getId(), "setAlpha", 0);
                views.setViewPadding(item.getId(), item.getPaddingLeft(),
                        item.getPaddingTop(), item.getPaddingRight(),
                        item.getPaddingBottom());
                if (bitmap != null) {
                    views.setImageViewBitmap(item.getId(), bitmap);
                } else {
                    views.setImageViewResource(item.getId(), _context.getApplicationInfo().icon);
                }
            } else if (item instanceof TextView) {
                // 隱藏最上層所有 TextView,避免與底層文字重疊
                views.setViewVisibility(item.getId(), View.GONE);
            }
        }
    });

    // 上層版面背景設為全透明
    views.setInt(view.getId(), "setBackgroundColor", Color.argb(0, 0, 0, 0));
    views.setViewPadding(view.getId(), 0, view.getPaddingTop(),
                         view.getPaddingRight(), view.getPaddingBottom());

    bar.contentView.addView(view.getId(), views);
}

走訪子檢視的遞迴工具,用一個簡短的 Visitor 介面把回呼邏輯外放,避免把判斷寫死在遞迴裡:

public interface ViewVisitor {
    void onFindView(View view);
}

void findView(ViewGroup group, ViewVisitor visitor) {
    for (int i = 0; i < group.getChildCount(); i++) {
        View view = group.getChildAt(i);
        if (visitor != null) {
            visitor.onFindView(view);
        }
        if (view instanceof ViewGroup) {
            findView((ViewGroup) view, visitor);
        }
    }
}

這個方案的代價

由於無法對自訂版面做精確定位,這條路的 icon 尺寸與版面間距,難免會和原廠系統樣式有些許偏差。

更根本的問題是:整個做法建立在「系統通知版面的 id 結構固定」這個假設上,而這恰恰是各版本、各品牌差異最大的地方。因此它適合的是「插件架構下必須換 icon、且能接受視覺上不完美」的過渡情境。若專案可以正常打包資源檔,仍應優先採用 NotificationCompat.Builder 搭配 setSmallIcon(),並遵循 Android 8.0 之後的通知渠道(Notification Channel)規範——那才是長遠可維護的做法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved