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

Android 通知技巧終章:進度條通知與自訂版面配置

編輯:關於 Android 程式設計

前面幾篇談過通知的建立、管理與刪除,也示範過如何打造符合使用者預期導航行為的通知。作為這個系列的最後一篇,這次來點更自由的玩法——把通知欄當成自己的畫布,在上面做出有動感的變化。

一、帶進度條的通知

通知裡可以放一個進度指示器,讓使用者知道目前正在進行的操作走到哪裡了。選擇哪一種形式,取決於你能不能估算出所需時間:

· 估得出來——用「確定性」(determinate)形式,也就是一條有明確起訖的 progress bar;
· 估不出來——用「不確定性」(indeterminate)形式,讓它持續轉動即可。

1. 固定總量的進度條

技術要點很簡單:呼叫 setProgress() 把進度指示器加進通知,然後在背景執行緒裡定時更新並重新 notify。

final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        .setContentInfo(String.valueOf(++progressNum))
        .setContentTitle("Picture Download")
        .setContentText("Download in progress")
        .setDefaults(Notification.DEFAULT_ALL)
        .setLargeIcon(icon)
        .setSmallIcon(R.drawable.stat_notify_gmail)
        .setTicker("Progress Notification")
        .setOngoing(true);

// Start a lengthy operation in a background thread
new Thread(new Runnable() {
    @Override
    public void run() {
        int incr;
        // Do the "lengthy" operation 20 times
        for (incr = 0; incr <= 100; incr += 5) {
            builder.setProgress(100, incr, false);
            mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Log.d(TAG, "sleep failure");
            }
        }
        builder.setContentText("Download complete")
               .setProgress(0, 0, false)
               .setOngoing(false);
        mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build());
    }
}).start();

這裡有兩個容易忽略的關鍵。第一,更新進度時必須用同一個 notification ID,否則每次都會多一條通知出來,而不是在原地刷新。第二,做完之後要呼叫 setProgress(0, 0, false) 把進度條收掉,並把 setOngoing(false),這樣使用者才能用手勢把通知滑掉。

2. 不確定型態的轉圈指示器

技術要點:呼叫 setProgress(0, 0, true),前兩個參數會被忽略,第三個參數 indeterminate 設為 true 就是無限轉動的形式。

builder.setProgress(0, 0, true);
mNotiMgr.notify(0, mBuilder.build());

二、自訂樣式的通知

通知框架允許你完全自woodwork版面配置。做法是在一個 RemoteViews 物件裡定義通知的樣式,而這個樣式同樣寫在 XML 版面檔中。

自訂通知與一般通知的差別在於:不再呼叫 setContentTitle() 這類方法逐項設定文字,而是改用 setContent() 直接把整個 RemoteViews 塞進去。

有兩個限制必須記住:高度方面,標準 view 上限 64dp,展開 view 上限 256dp;另外不要在 RemoteViews 裡設定 Drawable 背景,因為通知欄的底色由系統決定,你的文字顏色很可能因此看不清。

實作步驟

(1) 為通知準備一個獨立的 XML 版面檔;
(2) 用 RemoteViews 搭配 setOnClickPendingIntent() 綁定子元件的點擊行為;
(3) 透過 setContent() 把 RemoteViews 放進 Builder。

專案結構:

建立並發出自訂通知的方法:

/**
 * 自定義樣式通知
 */
private void showCustomNoti() {
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.custom);

    Intent intent = new Intent(INTENT_ACTION);
    intent.putExtra("isPlay", true);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.play_pause_music, pendingIntent);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setContent(views)
            .setDefaults(Notification.DEFAULT_ALL)
            .setLargeIcon(icon)
            .setSmallIcon(R.drawable.music_icon)
            .setTicker("Custom Notification")
            .setOngoing(true);

    mNotiMgr.notify(CUSTOM_NOTI_ID, builder.build());
}

對應的版面檔 custom.xml,做了一個音樂播放器的樣子——左邊專輯封面,中間歌名與歌手,右邊三顆控制鈕:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/songer"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:src="@drawable/songer" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:layout_weight="1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/song_name"
            android:textSize="18sp"
            android:gravity="center_horizontal" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/songer_name"
            android:textSize="14sp"
            android:gravity="center_horizontal" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/last_music"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:src="@drawable/music_previous" />

        <ImageView
            android:id="@+id/play_pause_music"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:src="@drawable/music_play" />

        <ImageView
            android:id="@+id/next_music"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:src="@drawable/music_next" />
    </LinearLayout>

</LinearLayout>

三、收尾

自訂通知的強大之處在於它把「通知」從單純的提示變成了一個 miniature 的操作介面——使用者不用把 App 切到前景,就能完成播放、暫停、切歌這些動作。

但自由度也伴隨成本:RemoteViews 只支援有限的 View 種類,不能用它執行自訂的绘制邏輯;而且一旦系統升級調整了通知欄的視覺規範,你的自訂版面很可能就與其他通知格格不入。所以實務上的建議是——能用標準樣式解決的,就別急著自訂。

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