編輯:關於 Android 程式設計
本文以實例完整講解 Android 中 Notification 的用法,供需要的朋友參考。
Notification 就是「通知」的意思。在 Android 裡,它指的是通知欄機制,常用於電話、短信、郵件、鬧鐘等情境:手機狀態欄上會出現一個小圖示提醒使用者有新的訊息,這時從螢幕上方滑下狀態欄,就能展開並處理這則通知。
官方幫助文件是這樣描述的:Notification 類別代表一則持續性的通知,會提交給使用者,並透過 NotificationManager 來管理;自從加入了 Notification.Builder 之後,建構通知變得更加容易。
簡單來說,Notification 讓你的應用即使在未開啟或於背景執行時,也能提醒使用者有需要注意的事件發生。對於那些看不見的程式元件(Broadcast Receiver、Service 或未啟用的 Activity)而言,這是提醒使用者最理想的方式。
先釐清「狀態條」與「狀態欄」的差別:
1、狀態條就是手機螢幕最上方那條長條狀的區域,上面顯示許多資訊,例如 USB 連線圖示、手機訊號圖示、電池電量圖示、時間等等;

2、狀態欄則是從狀態條往下拉、可以伸縮的 view;狀態欄中一般有兩類內容(以 FLAG 標記):(1)正在進行的程式;(2)通知事件。

一則 Notification 所傳遞的資訊包括:
1、一個狀態條圖示;
2、在展開的狀態欄視窗中顯示帶有大標題、小標題與圖示的資訊,並可處理點擊事件,例如呼叫該程式的入口類別;
3、閃光、LED 或震動等提醒。
下面簡單介紹一下 Notification 類別中幾個常見的常數:
常數:
DEFAULT_ALL 使用所有預設值,例如聲音、震動、閃屏等
DEFAULT_LIGHTS 使用預設閃光提示
DEFAULT_SOUNDS 使用預設提示聲音
DEFAULT_VIBRATE 使用預設手機震動
注意:若要加入手機震動效果,一定要在專案清單檔中加入震動權限:
複製代碼 程式碼如下:<uses-permission android:name="android.permission.VIBRATE" />
下面透過一個簡單的小實例來實現 Notification 功能:
MainActivity.java:
package com.example.lession16_notifi; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.res.Resources.NotFoundException; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { private NotificationManager notificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 第一步:通過getSystemService()方法得到NotificationManager對象; notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } // 測試 public void test1(View v) { showNotification("來短信了", "5557", "hello!", R.drawable.ic_launcher, R.drawable.ic_launcher); } // 第二步:對Notification的一些屬性進行設置比如:內容,圖標,標題,相應notification的動作進行處理等等; public void showNotification(String tickerText, String contentTitle, String contentText, int iconId, int notiId) { // 創建一個Notification Notification notification = new Notification(); // 設置通知 消息 圖標 notification.icon = iconId; // 設置發出消息的內容 notification.tickerText = tickerText; // 設置發出通知的時間 notification.when = System.currentTimeMillis(); // 設置顯示通知時的默認的發聲、振動、Light效果 notification.defaults = Notification.DEFAULT_VIBRATE;// 振動 // Notification notification = new Notification(R.drawable.ic_launcher,"有新的消息", System.currentTimeMillis()); // 3步:PendingIntent android系統負責維護 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,getIntent(), 0); // 4步:設置更加詳細的信息 notification.setLatestEventInfo(this, contentTitle, contentText,pendingIntent); // 5步:使用notificationManager對象的notify方法 顯示Notification消息 需要制定 // Notification的標識 notificationManager.notify(notiId, notification); } // 6步:使用notificationManager對象的cancelAll()方法取消 public void clearNoti(View v) { notificationManager.cancelAll();// 清除所有 } }
布局檔案 activity_main.xml:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="22dp" android:onClick="test1" android:text="@string/text_notifi" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="60dp" android:onClick="clearNoti" android:text="@string/text_clear" /> </RelativeLayout>
布局效果:

切記,若要實現震動效果,務必在清單檔中加入權限:
複製代碼 程式碼如下:<uses-permission android:name="android.permission.VIBRATE" />
實現效果如下:


整體流程可以濃縮成幾個關鍵步驟:先透過 getSystemService 取得 NotificationManager,接著建立 Notification 並設定圖示、內容與提醒方式,再用 PendingIntent 指定點擊後要開啟的頁面,最後呼叫 notify() 發出通知、用 cancelAll() 清除。掌握這幾步,你就能替自己的 App 加上完善的通知體驗了。
希望本文所述能對你的 Android 程式設計有所幫助。
Android 系統內建螢幕錄製:用 screenrecord 錄成 MP4
不管是做教學還是產品演示,能把 Android 手機或平板的螢幕錄成影片,都是件很實用的事。早期只能靠連續截圖再拼成影片,效果極差。從 Android 4.4(KitKa
Android Touch 系統(二):實例詳解 onInterceptTouchEvent 與 onTouchEvent 的調用過程
上一篇文章講述了 Android TouchEvent 的分發過程,其中有兩個重要函數:onInterceptTouchEvent 和 onTouchEvent,它們都可
用 SharedPreferences 保存零散資料:Android 儲存學習筆記
前面幾篇我們一直用純文字檔來存使用者資訊,這種做法有兩個明顯毛病:格式全靠自訂(例如自己拿 ## 當分隔符),既容易出錯也不好維護;再者檔案內容缺乏結構,日後要擴充欄位幾
仿 360 懸浮窗效果實現詳解
今天我們來聊聊如何模仿 360 安全衛士的懸浮窗。 說實話,360 安全衛士雖然「流氓」,但安裝的人著實不少。它有個讓人頗為無奈的功能:即使你關閉了 360 安全衛士,手