編輯:關於 Android 程式設計
手機收到簡訊時,即便沒有點開,最上方狀態列也會出現一個小圖示提示——這就是 Notification 的效果。本文整理三種不同 API 時期的通知建立方式,並示範自訂通知版面與清除通知的做法。
由於 Android 版本演進快速,Notification 的 API 經歷過多次更迭,有些寫法已被標記為不建議使用。以下範例刻意保留舊寫法,方便維護老專案時對照。



無論哪一種寫法,第一步都是從系統服務中取得 NotificationManager,它是所有通知發送的入口。
public class MainActivity extends Activity {
private static final int NOTIFICATION_FLAG = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notificationMethod(View view) {
// NotificationManager 是一個系統 Service
NotificationManager manager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
switch (view.getId()) {
// 寫法一:直接 new Notification(),已被標記為不建議,但相容性最好
case R.id.btn1:
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
Notification notify1 = new Notification();
notify1.icon = R.drawable.message;
notify1.tickerText = "您有新短訊息,請注意查收!";
notify1.when = System.currentTimeMillis();
notify1.setLatestEventInfo(this, "Notification Title",
"This is the notification message", pendingIntent);
notify1.number = 1;
// 點擊後自動清除
notify1.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTIFICATION_FLAG, notify1);
break;
// 寫法二:Notification.Builder,API 11 之後可用
case R.id.btn2:
PendingIntent pendingIntent2 =
PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
Notification notify2 = new Notification.Builder(this)
.setSmallIcon(R.drawable.message) // 建議尺寸 24x24
.setTicker("您有新短訊息,請注意查收!")
.setContentTitle("Notification Title")
.setContentText("This is the notification message")
.setContentIntent(pendingIntent2)
.setNumber(1)
.getNotification(); // API 11 用 getNotification()
notify2.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTIFICATION_FLAG, notify2);
break;
// 寫法三:Builder + build(),API 16 之後可用
case R.id.btn3:
PendingIntent pendingIntent3 =
PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
Notification notify3 = new Notification.Builder(this)
.setSmallIcon(R.drawable.message)
.setTicker("您有新短訊息,請注意查收!")
.setContentTitle("Notification Title")
.setContentText("This is the notification message")
.setContentIntent(pendingIntent3)
.setNumber(1)
.build(); // build() 自 API 16 起提供
notify3.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTIFICATION_FLAG, notify3);
break;
// 寫法四:RemoteViews 自訂版面
case R.id.btn4:
Notification myNotify = new Notification();
myNotify.icon = R.drawable.message;
myNotify.tickerText = "您有新短訊息,請注意查收!";
myNotify.when = System.currentTimeMillis();
myNotify.flags = Notification.FLAG_NO_CLEAR; // 不允許自動清除
RemoteViews rv = new RemoteViews(getPackageName(), R.layout.my_notification);
rv.setTextViewText(R.id.text_content, "hello world!");
myNotify.contentView = rv;
Intent intent = new Intent(Intent.ACTION_MAIN);
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);
myNotify.contentIntent = contentIntent;
manager.notify(NOTIFICATION_FLAG, myNotify);
break;
// 清除通知
case R.id.btn5:
manager.cancel(NOTIFICATION_FLAG);
// 若要一次清掉全部:manager.cancelAll();
break;
}
}
}
通知並不是立刻被點擊的,使用者可能幾小時後才下拉狀態列。因此不能直接傳入 Intent,而要用 PendingIntent 把「未來某個時刻要執行的操作」包裝起來,交由系統代為觸發。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="notificationMethod" android:text="預設通知(已棄用但通用)" />
<Button android:id="@+id/btn2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="notificationMethod" android:text="預設通知(API 11 之後)" />
<Button android:id="@+id/btn3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="notificationMethod" android:text="預設通知(API 16 之後)" />
<Button android:id="@+id/btn4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="notificationMethod" android:text="自訂通知" />
<Button android:id="@+id/btn5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="notificationMethod" android:text="清除通知" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical" >
<TextView android:id="@+id/text_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
實務提醒:FLAG_AUTO_CANCEL 讓通知在使用者點擊後自動消失,FLAG_NO_CLEAR 則相反,需要自行呼叫 cancel。另外,同一個 notify id 會覆蓋前一則通知,若要並存多則訊息,請給不同的 id。
Android 中實作 TCP 與 UDP 傳輸的完整示例
從 TCP 到 UDP:Android 網路傳輸實作解析 TCP 與 UDP 是行動應用開發中最常見的傳輸層協定。在 Android 環境中,兩者的使用方式與 Java
Android 自定義狀態欄顏色,讓它與標題欄保持一致
用 iOS 時會注意到,狀態欄的顏色總能與 App 的標題欄融為一體,質感很好。這種好奇驅使我去查:Android 行不行?答案是——4.4 之前不行,4.4 之後可以了
GreenDAO 自訂型別轉換器與 ProGuard 混淆設定實作指南
在實體類(Entity)裡,難免會出現一些欄位在資料庫中找不到對應的型別。這種時候就得靠自訂的型別轉換器來搭橋。最典型的例子就是 Date:如果對精度要求不高,多數人會把
深入透徹理解 Android Activity 橫豎屏切換的生命周期
開發中經常需要處理橫豎屏切換,而想妥善處理,就得先摸清切換時的生命周期變化。 兩個關鍵方法 Activity 橫豎屏切換時會回調兩個方法,可視為生命周期的一部分: on