編輯:關於 Android 程式設計
Notification 中文即為「通知」,通常用來顯示廣播式的提示訊息。要使用 Notification,就必須搭配 NotificationManager。本文透過實例說明它的完整用法。
①取得系統服務 NotificationManager,呼叫 Context.getSystemService(NOTIFICATION_SERVICE) 即可取得實例。
②建立 Notification 並設定其屬性,透過建構子 public Notification(int icon, CharSequence tickerText, long when) 建立實例。
③透過 NotificationManager 發出通知,它提供 notify() 發出通知、cancel(...) 取消通知兩個方法。
先初始化 notificationManager 與 notification 兩個成員變數:
notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); notification = new Notification(R.drawable.touxiang,"信息",System.currentTimeMillis());
Notification 建構子的三個參數依序是圖示、標題文字「信息」、以及系統時間,如圖一所示:

(圖一)

(圖二)
版面只是兩顆按鈕,重點看發送按鈕:
sendButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(NotificationActivity.this,NotificationActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0); notification.setLatestEventInfo(NotificationActivity.this, "你的一條信息", "來自張三的信息", pendingIntent); notificationManager.notify(ID,notification); notificationManager.notify(ID+1, notification); } })setLatestEventInfo(...) 的效果如圖二:通知裡出現兩條一模一樣的訊息,這是因為我用不同的 ID 呼叫了兩次 notify()。ID 是 int 型的通知標識符,即使內容相同,只要 ID 不同,Android 就會顯示為兩條獨立通知。
再來說明 pendingIntent 的角色:它並非立即意圖,而是可在別處延後執行的意圖。當使用者點擊展開的通知時,pendingIntent 才會真正啟動——例如圖二中點擊訊息後,立刻跳進另一個 Activity。
除了圖示與標題,通知還能設定提示音、震動、閃光燈等。以下為完整程式碼:
package com.study.android; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button startBtn; private Button cancelBtn; private static final int HELLO_ID = 1; NotificationManager mNotificationManager; Notification mNotification; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startBtn = (Button)findViewById(R.id.startBtn); cancelBtn = (Button)findViewById(R.id.cancelBtn); // ① 獲取NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; mNotificationManager = (NotificationManager)this.getSystemService(ns); // ② 初始化Notification int icon = R.drawable.ic_launcher; CharSequence tickerText = "Hello"; long when = System.currentTimeMillis(); mNotification = new Notification(icon,tickerText,when); mNotification.defaults = Notification.DEFAULT_ALL; mNotification.flags |= Notification.FLAG_NO_CLEAR; mNotification.flags |= Notification.FLAG_SHOW_LIGHTS; // ③ 定義notification的消息 和 PendingIntent Context context = this; CharSequence contentTitle ="My notification"; CharSequence contentText = "Hello World"; Intent notificationIntent = new Intent(this,MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,0 ); mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); // ④ 把封裝好的notification傳入NotificationManager // 開啟通知 startBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mNotificationManager.notify(HELLO_ID,mNotification); } }); // 取消通知 cancelBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mNotificationManager.cancel(HELLO_ID); } }); } }以上就是 Notification 的基本用法。掌握「取得 Manager→建立 Notification→設定 PendingIntent→發出通知」這條主線,再搭配提示音與震動等屬性,就能勝任絕大多數通知情境。
Android Activity 學習筆記
什麼是 Activity Activity 是 Android 應用程式中負責使用者互動的元件。它通常提供一個畫面視窗,開發者可透過 setContentView() 將
Android 布局標簽 Include 與 Merge 使用指南
為什麼需要可重用布局 Android 提供了許多輕量級元件,但在實際專案中,常需要重用較大的布局片段,例如標題列、按鈕面板或自訂進度條。把共同部分抽成獨立布局,可以讓維護
Android PopupWindow 多方向菜單:左側、右側與底部彈出實現
多方向菜單PopupWindow 不只可以從按鈕下方彈出,也能依需求從左側、右側或底部出現。實作時可透過列舉或變數記錄目前要顯示的方向,再依方向決定視窗寬高、動畫與顯示位
移動端 HTML5:去掉 input 日期輸入框的默認白底與邊框(兼容 Android 與 iOS)
前兩天做一個在微信內打開的 HTML5 頁面,其中有個訂單查詢需要選擇日期。一開始直接用了 <input type=date>,沒加任何樣式,結果渲染出來是白