編輯:Android 開發實例
本篇整理自 Android 開發者指南中 Framework Topics / User Interface / Notifications / Status Bar Notifications 章節,對應版本為 Android 4.0 r1,內容在原始翻譯基礎上重新編排。
快速導覽:狀態欄(status bar)通知允許應用程式以不干擾當前 Activity 的方式將事件告知使用者,並可為通知綁定一個 Intent,當使用者點擊時系統便會執行該 Intent。
狀態欄通知會在系統的狀態欄中加入一個圖示(可搭配一段提示文字),並在通知視窗中加入一筆展開資訊。當使用者選取展開資訊時,Android 便會執行通知所定義的 Intent(通常用來彈出一個 Activity)。此外,你還可以設定用裝置提供的聲音、振動與閃光來提醒使用者。
當後台服務(Service)需要對某事件發出提醒並等待使用者回應時,狀態欄通知就能派上用場。由於後台服務從來不會直接啟動 Activity 來接收互動,因此正確做法是建立一個狀態欄通知,待使用者點選後再由通知啟動 Activity。
Activity 與 Service 都能發出狀態欄通知,但由於 Activity 只有在活躍並取得焦點時才能操作,因此更建議用 Service 來建立通知,這樣即使使用者正在使用其他程式或裝置已休眠,仍能從後台發出提醒。整個機制需要用到兩個類別:Notification 與 NotificationManager。
Notification 類別的實例用來定義通知的各項屬性,例如圖示、展開資訊與聲音等附屬設定;NotificationManager 則是管理並運行所有通知的系統服務。由於它無法被直接實例化,需透過 getSystemService() 取得參考,再呼叫 notify() 把 Notification 物件傳入。
1. 取得 NotificationManager 的參考:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(ns);
2. 實例化 Notification:
int icon = R.drawable.notification_icon; CharSequence tickerText = "Hello"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when);
3. 指定展開資訊與 Intent:
Context context = getApplicationContext(); CharSequence contentTitle = "My notification"; CharSequence contentText = "Hello World!"; Intent notificationIntent = new Intent(this, MyClass.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
4. 把 Notification 傳給 NotificationManager:
private static final int HELLO_ID = 1; mNotificationManager.notify(HELLO_ID, notification);
完成以上步驟,使用者就能看到通知了。
所有通知都由 NotificationManager 管理,只能透過 getSystemService() 取得。要發送通知,只要呼叫 notify(int, Notification) 即可:第一個參數是通知的唯一 ID,第二個參數是通知物件本身。ID 在整個應用範圍內唯一標識通知,之所以需要它,是因為通知需要更新,且應用可能同時管理多種不同的通知,必須能在使用者返回應用時執行正確的動作。
若希望使用者從通知視窗點選後自動清除通知,可在通知中加入 FLAG_AUTO_CANCEL 標志;也可以傳入通知 ID 呼叫 cancel(int) 手動清除,或以 cancelAll() 一次清除所有由你建立的通知。
狀態欄通知必須包含:狀態欄圖示、展開視圖的標題與內容(除非使用自訂展開視圖),以及一個在通知被點選時執行的 PendingIntent。可選設定則包含狀態欄提示文字、提醒聲音、振動設定與 LED 閃光設定。
應用程式可以在事件進行中更新狀態欄通知。例如前一則簡訊尚未讀取、又來了新簡訊,簡訊程式為了正確顯示未讀總數,可以更新既有通知。相對於每次都新增一筆通知,更新原有通知更能避免通知視窗的混亂。由於每個通知都由整數 ID 唯一標識,只需以 setLatestEventInfo() 修改內容,再呼叫 notify() 即可更新。
可使用使用者預設的提示音,或指定應用程式自己的音效。使用預設提示音時,為 defaults 屬性加上 DEFAULT_SOUND:
notification.defaults |= Notification.DEFAULT_SOUND;
若要指定音效,則將 Uri 傳給 sound 屬性,例如:
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
若希望聲音在使用者回應前持續循環播放,可將 FLAG_INSISTENT 加入 flags。需特別注意:若 defaults 已包含 DEFAULT_SOUND,預設提示音將覆蓋 sound 屬性所指定的聲音。
使用預設振動模式時,為 defaults 加上 DEFAULT_VIBRATE;若要自訂,則傳入一個 long 陣列給 vibrate 屬性:
long[] vibrate = {0, 100, 200, 300};
notification.vibrate = vibrate;
陣列依序定義了振動「關、開、關、開」的時間(毫秒):第一個數字是啟動前的等待時間,第二個是開啟振動的持續時間,以此類推。同樣地,若 defaults 已包含 DEFAULT_VIBRATE,預設模式將覆蓋自訂設定。
使用預設閃光時,為 defaults 加上 DEFAULT_LIGHTS。若要自訂顏色與節奏,則需指定 ledARGB(顏色)、ledOffMS(熄滅毫秒數)與 ledOnMS(亮起毫秒數),並在 flags 中加入 FLAG_SHOW_LIGHTS:
notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300; notification.ledOffMS = 1000; notification.flags |= Notification.FLAG_SHOW_LIGHTS;
上例實現了綠光閃爍 300 毫秒、間隔 1 秒的效果。由於各裝置的 LED 未必支援所有顏色,硬體會以最接近的顏色發光,其中綠色是最常見的提醒色。
利用 Notification 的屬性與標志,還能賦予通知更多能力:
預設情況下,通知視窗的展開視圖只包含基本的標題與文字,由 setLatestEventInfo() 的 contentTitle 與 contentText 指定。若想自訂版面,可利用 RemoteViews 來定義。以下為完整流程:
1. 建立展開視圖的 XML 版面(例如 custom_notification_layout.xml):
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp">
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp" />
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />
</LinearLayout>
2. 在程式碼中以 RemoteViews 定義圖片與文字,並傳給 contentView 屬性:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout); contentView.setImageViewResource(R.id.image, R.drawable.notification_image); contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view"); notification.contentView = contentView;
3. 由於自訂視圖無需呼叫 setLatestEventInfo(),必須另行以 contentIntent 屬性定義通知要執行的 Intent:
Intent notificationIntent = new Intent(this, MyClass.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.contentIntent = contentIntent;
4. 最後照常發送通知即可:
mNotificationManager.notify(CUSTOM_VIEW_ID, notification);
此外,RemoteViews 也提供了在展開視圖中加入 Chronometer 或 ProgressBar 的方法。
注意:建立自訂展開視圖時,務必確保版面在不同螢幕方向與解析度下都能正常運作。由於通知可用的螢幕區域相當有限,切勿把版面設計得過於複雜,並應在各種配置下充分測試。
Android 多媒體播放:MediaPlayer 控制音樂
Android 的 MediaPlayer 類別可以控制音訊與影片播放。開發者可以透過它實作播放、暫停、快轉、倒退與音量控制等功能。 建立 MediaPlayer 將音樂
Android 檔案搜尋實作:在 SD 卡中遞迴查找指定檔名
做檔案管理器或需要掃描外部儲存空間時,「找出所有符合關鍵字的檔案」幾乎是必備功能。本文示範一個簡潔的實作:使用者輸入搜尋目錄與關鍵字,程式遞迴走訪底下所有子目錄,把命中的
Android NFC 開發實戰:以 MifareClassic 卡片讀寫為例
現在市面上大多數 Android 手機都已經具備 NFC(近距離無線通訊)能力。本文用一個完整可跑的例子,帶你走過在 Android 上讀取 NFC 卡片資料的整個流程。
Android 網路資料解析:使用 JSONObject 讀取 JSON
在 Android 應用中,從網路取得資料後,常需要把 JSON 字串轉成可用物件。本文以天氣 API 為例,說明如何下載資料並解析。 解析流程 確認感興趣的欄位。