編輯:關於 Android 程式設計
在 Android 應用裡,通知(Notification)是應用與使用者溝通最直接的一條通道。本文以一個可運行的範例為主線,把 Notification 的常用常量、標誌位、關鍵字段以及 NotificationManager 的核心方法梳理清楚,並附上完整的實作程式碼,方便直接取用。
最常見的思路是順著 Activity 生命週期來做。當 Activity 不可見時,系統會呼叫 onStop()——比如使用者按下 Home 鍵——此時把通知放進狀態列;等使用者再次回到前台,於 onStart() 中把通知撤掉。這樣通知的存在感就與「程式正在背景執行」這件事綁在一起。
另一種策略更簡單:只要應用進程還活著,就一直保留狀態列圖示,讓使用者隨時知道服務仍在運行。兩種做法沒有絕對優劣,取決於產品對「可見性」的定義。
常量:
DEFAULT_ALL 使用所有預設效果,包含聲音、震動、閃光等
DEFAULT_LIGHTS 使用預設閃光提示
DEFAULT_SOUNDS 使用預設提示音
DEFAULT_VIBRATE 使用預設手機震動
【說明】若想啟用震動,務必在 manifest.xml 中宣告權限:
<uses-permission android:name="android.permission.VIBRATE" />
這些效果常量可以用位元或運算任意疊加:
notification.defaults = DEFAULT_SOUND | DEFAULT_VIBRATE;
也可以在既有基礎上追加:
notification.defaults |= DEFAULT_SOUND;
提醒:震動效果建議在真機上驗證,模擬器通常不會呈現震動回饋。
FLAG_AUTO_CANCEL 使用者點擊通知後,通知自動從狀態列移除
FLAG_NO_CLEAR 通知無法被狀態列的「清除全部」按鈕清掉
FLAG_ONGOING_EVENT 把通知歸入「正在進行」分組
FLAG_INSISTENT 持續提醒直到使用者回應,例如音樂播放中的提示
實務上 FLAG_ONGOING_EVENT 常與 FLAG_NO_CLEAR 搭配使用,讓「正在運行」類的通知不被使用者誤清。
contentIntent 設定 PendingIntent,點擊通知時觸發
defaults 附加預設提醒效果
flags 設定標誌位,例如 FLAG_NO_CLEAR
icon 通知圖示資源
sound 自訂提示音
tickerText 狀態列滾動顯示的文字
when 通知的時間戳
public void cancelAll() 移除當前 Context 下的所有通知
public void cancel(int id) 移除指定 id 的通知
public void notify(String tag, int id, Notification notification) 以 tag 標籤與 id 標記發送通知
public void notify(int id, Notification notification) 以 id 標記發送通知
package com.ljq.activity; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); clearNotification(); } @Override protected void onStop() { showNotification(); super.onStop(); } @Override protected void onStart() { clearNotification(); super.onStart(); } /** * 在狀態列顯示通知 */ private void showNotification(){ // 建立 NotificationManager 的參考 NotificationManager notificationManager = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 定義 Notification 的各項屬性 Notification notification =new Notification(R.drawable.icon, "督導系統", System.currentTimeMillis()); //FLAG_AUTO_CANCEL 點擊後通知自動被狀態列清除掉 //FLAG_NO_CLEAR 通知不能被狀態列的清除按鈕清掉 //FLAG_ONGOING_EVENT 通知放置在「正在運行」 //FLAG_INSISTENT 是否持續進行,例如音樂一直播放直到使用者回應 notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放入通知欄的「Ongoing」即「正在運行」組中 notification.flags |= Notification.FLAG_NO_CLEAR; // 表示點擊「清除通知」後此通知仍不清除,常與 FLAG_ONGOING_EVENT 搭配 notification.flags |= Notification.FLAG_SHOW_LIGHTS; //DEFAULT_ALL 使用所有預設值,比如聲音、震動、閃屏等等 //DEFAULT_LIGHTS 使用預設閃光提示 //DEFAULT_SOUNDS 使用預設提示聲音 //DEFAULT_VIBRATE 使用預設手機震動,需加上 <uses-permission android:name="android.permission.VIBRATE" /> 權限 notification.defaults = Notification.DEFAULT_LIGHTS; //疊加效果常量 //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND; notification.ledARGB = Color.BLUE; notification.ledOnMS =5000; //閃光時間,毫秒 // 設定通知的事件訊息 CharSequence contentTitle ="督導系統標題"; // 通知欄標題 CharSequence contentText ="督導系統內容"; // 通知欄內容 Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 點擊該通知後要跳轉的 Activity PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, contentTitle, contentText, contentItent); // 把 Notification 傳遞給 NotificationManager notificationManager.notify(0, notification); } //刪除通知 private void clearNotification(){ // 啟動後刪除我們之前定義的通知 NotificationManager notificationManager = (NotificationManager) this .getSystemService(NOTIFICATION_SERVICE); notificationManager.cancel(0); } }
需要留意的是,setLatestEventInfo() 與 new Notification(...) 這套寫法屬於早期 API 的產物,在 Android 8.0 之後已被通知管道(NotificationChannel)與 Builder 模式取代。若你的專案仍停留在舊版 SDK,上述範例可以直接運行;若是新專案,建議改用 NotificationCompat.Builder 並建立 NotificationChannel。
更多 Android 相關內容有興趣的讀者,可參考本站專題:《Android 基本組件用法總結》、《Android 視圖 View 技巧總結》、《Android 資源操作技巧匯總》、《Android 檔案操作技巧匯總》、《Android 操作 SQLite 資料庫技巧總結》、《Android 操作 json 格式資料技巧總結》、《Android 資料庫操作技巧總結》、《Android 程式開發之 SD 卡操作方法匯總》、《Android 開發入門與進階教程》、《Android 程式之 activity 操作技巧總結》及《Android 控制元件用法總結》。
希望本文所述對大家的 Android 程式設計有所幫助。
Android 開發者面試總結:65K、HashMap、性能與架構
面試中的 65K 限制 65K 限制來自 Dex 檔中方法索引使用 short 型態,導致單一 Dex 的方法數不能無限增加。常見解法包括移除冗餘套件、拆分功能、使用 m
Android 4.2.2 SurfaceFlinger 啟動流程詳解(一)
延續前面幾篇介紹 Android Framework 源碼閱讀基礎知識(主要與 C++ 相關)的文章,本篇開始將和大家一起學習並總結 SurfaceFlinger 模組在
從源碼層面徹底搞懂 Handler 的運行機制
Handler 幾乎是每个 Android 開發者都耳熟能詳的名字,但真正能把它的執行原理講清楚的人卻不多。今天我們不妨從源碼出發,一步步拆解 Handler 背後的運作
ZXing 掃描畫面被拉伸的解法:修正相機解析度計算
ZXing 是 Google 提供的二維碼掃描專案,開箱即用,但它的預設參數是為橫屏設計的。一旦把 Activity 轉成豎屏,預覽畫面就會出現明顯的橫向拉伸。問題根源在