<1>簡介
Android系統的狀態列(Status Bar)中有一個創新UI設計,這就是可以下拉的通知提示。當系統有一些訊息要通知使用者時,例如,收到簡訊、電子郵件、有未接來電時,都會把資訊作為通知(Notification)發送給使用者。
Status Bar 增加了一個表徵圖到系統狀態列中,還有文本資訊(可以不選),增加Notification資訊到Notification視窗。你還可以安裝Notification利用的聲音,震動,裝置上閃關燈來提醒使用者。
Notification與Toast都可以起到通知、提醒的作用。但它們的實現原理和表現形式卻完全不一樣。
Toast其實相當於一個組件(Widget)。有些類似於沒有按鈕的對話方塊。而Notification是顯示在螢幕上方狀態列中的資訊。
Notification需要用NotificationManager來管理,而Toast只需要簡單地建立Toast對象即可。
<2>步驟:
建立 status bar notification:
1。NotificationManager得到一個參考:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
或者
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2。建立一個Notification對象。每一個Notification對應一個Notification對象。在這一步需要設定顯示在螢幕上方狀態列的通知訊息、通知訊息前方的映像資源ID和發出通知的時間。一般為目前時間。
int icon = R.drawable.notification_icon; //顯示在螢幕上方狀態列的表徵圖
CharSequence tickerText = "Hello"; //顯示在螢幕上方狀態列的通知訊息
long when = System.currentTimeMillis(); //發出通知的時間
Notification notification = new Notification(icon, tickerText, when);
3。定義的資訊和PendingIntent通知:
由於Notification可以與應用程式脫離。也就是說,即使應用程式被關閉,Notification仍然會顯示在狀態列中。當應用程式再次啟動後,又可以重新控制這些Notification。如清除或替換它們。因此,需要建立一個PendingIntent對象。該對象由Android系統負責維護,因此,在應用程式關閉後,該對象仍然不會被釋放。
使用Notification類的setLatestEventInfo方法設定Notification的詳細資料。
Context context = getApplicationContext();
CharSequence contentTitle = "My notification"; //Notification通知標題
CharSequence contentText = "Hello World!"; //Notification通知內容
Intent notificationIntent = new Intent(this, MyClass.class); //跳轉到MyClass.class
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); //產生一個PendingIntent
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); //很重要的方法
4。通過對NotificationManager通知:
使用NotificationManager類的notify方法顯示Notification訊息。在這一步需要指定標識Notification的唯一ID。這個ID必須相對於同一個NotificationManager對象是唯一的,否則就會覆蓋相同ID的Notificaiton()
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
在這裡要解釋一下的是notify方法的第1個參數。這個參數實際上表示了Notification的ID。是一個int類型的值。
為了使這個值唯一,可以使用res目錄中的某些資源ID。例如,在上面的代碼中使用了當前Notification顯示的映像對應的資源ID(R.drawable.icon)作為Notification的ID。當然,讀者也可以使用其他的值作為Notification的ID值。
5。在顯示Notification時還可以設定顯示通知時的預設發聲、震動和Light效果。要實現這個功能需要設定Notification類的defaults屬性,代碼如下:
1 添加聲音:
你可以提醒使用者提供了預設的通知聲音(這是由使用者定義)或應用程式特定的聲音
應用程式規定的聲音:
notification.defaults = Notification.DEFAULT_SOUND;
自訂:使用一個不同的聲音的Notification,通過一個Uri引用聲音的位置。下面的例子使用一個儲存到裝置的SD卡已知的音頻檔案:
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
2 增加震動:
你可以提醒使用者提供了震動(這是由使用者定義)或應用程式特定的震動模式
應用程式規定的震動模式:
notification.defaults = Notification.DEFAULT_VIBRATE;
定義你自己的震動模式,通過一系列的長值:
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
第一個值是等待要花費你多長時間(關閉)開始之前,第二個值是第一個震動的長度,三是下一個長度時,等等。花紋可只要你喜歡,但它不能被設定為重複。
3添加燈光閃爍
通知使用者發出閃光LED燈,你可以繼承預設t模式(如有),或定義自己的顏色和圖案的燈。
使用預設的燈光設定,加上DEFAULT_LIGHTS到defaults:
notification.defaults = Notification.DEFAULT_LIGHTS;
<3>更多功能 (利用 Flag)
notification.flags = Notification.FLAG_AUTO_CANCEL;
自動取消通知之後,這是選擇通知視窗。
notification.flags = Notification.FLAG_INSISTENT;
重複的聲響,直到使用者響應。
notification.flags = Notification.FLAG_NO_CLEAR;
表明通知不應該被“Clear notifications”按鈕清除。這是特別有用,如果你的notification正在通知。
範例:
package xiaosi.notification;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;public class NotificationActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.a,"你有短訊息了",System.currentTimeMillis()); notification.flags = Notification.FLAG_AUTO_CANCEL; Intent notificationIntent = new Intent(); PendingIntent pedingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "你有短訊息了", "祝你新婚快樂", pedingIntent); notificationManager.notify(R.drawable.a,notification); }}
package xiaosi.notification;import android.app.Activity;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); }}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Notification示範"/></LinearLayout>
點擊下載源碼:原始碼