編輯:關於 Android 程式設計
通知是應用程式在「不跳出前景」的前提下與使用者溝通的手段,由系統的通知管理器統一管理。它能做的事包括:在狀態列顯示圖示、在展開的通知面板顯示詳細資訊(可附 Intent)、閃爍 LED、啟動震動、播放鈴聲或媒體庫音檔。
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
透過它可以發送新通知、修改既有通知、或移除不再需要的通知。
Notification notification = new Notification(
R.drawable.icon,
"在 EoeAndroidReceiver1 中",
System.currentTimeMillis());
建構子需要三個參數:狀態列圖示、狀態列的提示文字(ticker text)、以及通知產生的時間。另外可設定 number 屬性,讓同一個圖示代表多個事件時顯示數量。
有兩種方式配置通知面板的外觀:
setLatestEventInfo() 填入標準版面所需的欄位——標題與詳細說明字串。標準版面會自動使用建構子中指定的圖示與時間。contentView 與 contentIntent,自訂整個面板 UI。notification.setLatestEventInfo(context, "在 EoeAndroidReceiver1 中", null, contentIntent);
通知的目的通常是引導使用者採取行動,所以要指定一個 PendingIntent,在點擊時觸發:
PendingIntent contentIntent = PendingIntent.getActivity(
context, 0, new Intent(context, ActivityMain.class), 0);
notification.setLatestEventInfo(context, "標題", "內容", contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
多數情況下這個 Intent 應該開啟 App 並導航到與通知內容相關的頁面。
使用系統預設值是最一致、最省事的做法,可任意組合:
notification.defaults = Notification.DEFAULT_LIGHTS
| Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE;
// 或全部採用
notification.defaults = Notification.DEFAULT_ALL;
自訂鈴聲:把音檔的 URI 指派給 sound 屬性。自訂音檔需事先放到裝置上,或放進 res/raw:
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound = uri;
// 或使用內建媒體
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
震動:指派一個 long 陣列給 vibrate 屬性,元素依序代表「震動時間」與「暫停時間」交替:
long[] vibrate = {1000, 1000, 1000, 1000, 1000};
notification.vibrate = vibrate;
使用震動必須在 manifest 中宣告權限:
<uses-permission android:name="android.permission.VIBRATE"/>
LED:可控制顏色與閃爍頻率。各裝置對 LED 的控制能力不同,若指定顏色不可用,系統會挑最接近的顏色。
notification.ledARGB = Color.RED;
notification.ledOnMS = 300; // 亮
notification.ledOffMS = 1000; // 滅
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
設定 LED 之後務必加上 FLAG_SHOW_LIGHTS,否則不會生效。若把 ledOnMS 設為 1、ledOffMS 設為 0 可讓 LED 長亮;兩者皆設 0 則關閉。
| 旗標 | 效果 |
|---|---|
| FLAG_AUTO_CANCEL | 點擊通知後自動清除 |
| FLAG_INSISTENT | 重複發出聲音,直到使用者回應(通常用於鬧鐘、來電) |
| FLAG_ONGOING_EVENT | 放入通知面板的「進行中」分組,如下載進度 |
| FLAG_NO_CLEAR | 點擊「清除通知」時不被移除,常與 FLAG_ONGOING_EVENT 搭配 |
| FLAG_SHOW_LIGHTS | 啟用 LED 設定 |
另外 number 屬性代表目前事件數量,會疊在狀態列圖示上方;若要使用,數值須從 1 開始。
// 1. 取得服務
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. 設定提示效果
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// 4. 設定點擊行為
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);
// 5. 發送
private static final int ID_NOTIFICATION = 1;
mNotificationManager.notify(ID_NOTIFICATION, notification);
更新:只要重新設定 notification 並用同一個 ID 再呼叫一次 notify(),就會覆蓋原通知——這也是實作下載進度的標準做法。
移除:呼叫 mNotificationManager.cancel(ID_NOTIFICATION) 取消單一通知,或用 cancelAll() 一次清除全部。
若標準版面無法滿足需求,可透過 RemoteViews 指定自訂 layout。官方文件(SDK 內 docs/guide/topics/ui/notifiers/notifications.html)對此有詳細說明。
本文 API 屬於早期 Android 的通知模型。從 Android 5.0 起改為 Builder 模式,8.0(API 26)更強制要求所有通知都必須指定 NotificationChannel,否則完全不會顯示;setLatestEventInfo() 也早已移除。若你在維護舊專案,上述寫法仍然有效;若為新專案,請直接採用 NotificationCompat.Builder 搭配 NotificationManagerCompat,並記得建立 channel 與處理通知權限請求。
重新認識 Android 中的 MVP 架構
前言 為什麼要談 MVP?網路上的討論已經很多,但核心理由其實一句話就能說清:MVC 裡的 Activity 揹了太多不屬於它的責任。當一個類同時負責版面、資料、網路請求
PopupWindow 模擬 iOS 底部彈窗
底部彈窗的應用在行動應用中,底部彈窗常用於提供操作選單,例如選擇頭像來源、分享管道或取消操作。它比中央對話框更貼近拇指操作,也符合多數使用者的習慣。PopupWindow
基於百度定位 SDK 實時計算移動距離的完整實現
前段時間因項目需求,我通過百度定位 SDK 實現了一個實時更新行駛距離的程序(類似出租車上的里程表)。看似簡單的「顯示當前位置」背後,要真正做到「實時累加移動距離」,其實
MPAndroidChart 折線圖實作詳解:初始化、資料設定與更新
MPAndroidChart 是 Android 上常用的圖表庫,支援折線圖、長條圖、圓餅圖等多種圖表。本文以折線圖為例,說明初始化、資料設定、更新與 x 軸資料處理方式