Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 入門:Notification 通知的用法分析

編輯:關於 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→發出通知」這條主線,再搭配提示音與震動等屬性,就能勝任絕大多數通知情境。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved