android NoticificationManager狀態欄操作

NoticificationManager很容易可以放在狀態欄,也很容易實現從statusbar進入程序 中,
NoticificationManager中通過intent執行此程序的activity就可以了

NoticificationManager狀態欄操作

NotificationManager(通知管理器):
NotificationManager負責通知用戶事件的發生.
NotificationManager有三個公共方法:
1. cancel(int id) 取消以前顯示的一個通知.假如是一個短暫的通知,試圖將隱藏,假如是一個持久的通知,將從狀態條中移走.
2. cancelAll() 取消以前顯示的所有通知.
3. notify(int id,  Notification notification) 把通知持久的發送到狀態條上.


//初始化NotificationManager:
NotificationManager nm =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification代表着一個通知.
Notification的屬性:
audioStreamType 當聲音響起時,所用的音頻流的類型
contentIntent 當通知條目被點擊,就執行這個被設置的Intent.
contentView 當通知被顯示在狀態條上的時候,同時這個被設置的視圖被顯示.
defaults 指定哪個值要被設置成默認的.
deleteIntent 當用戶點擊"Clear All Notifications"按鈕區刪除所有的通知的時候,這個被設置的Intent被執行.
icon 狀態條所用的圖片.
iconLevel 假如狀態條的圖片有幾個級別,就設置這裏.
ledARGB LED燈的顏色.
ledOffMS LED關閉時的閃光時間(以毫秒計算)
ledOnMS LED開始時的閃光時間(以毫秒計算)
number 這個通知代表事件的號碼
sound 通知的聲音
tickerText 通知被顯示在狀態條時,所顯示的信息
vibrate 振動模式.
when 通知的時間戳.

將Notification發送到狀態條上:
Notification notification = new Notification();
Notification的設置過程……..
nm.notify(0, notification); //發送到狀態條上

創建和觸發Notification

 創建和配置新的Notification需要經歷三步。
  
  首先,你要創建一個新的Notification對象,傳入要在狀態條上顯示的圖 標、文字和Notification的 當前時間,如下面的代碼片段所示:
  
  // Choose a drawable to display as the status bar icon
  int icon = R.drawable.icon;
  
  // Text to display in the status bar when the notification is launched
  String tickerText = “Notification”;
  
  // The extended status bar orders notification in time order
  long when = System.currentTimeMillis();
  Notification notification = new Notification(icon, tickerText, when);
  
  當Notification觸發時,文本將沿着狀態條進行滾動 顯示。

其次,使用setLatestEventInfo方法來配置Notification在擴展的狀態窗口中的外觀。擴展 的狀態窗口將顯示圖標和在構造函數中傳入的時間,以及顯示標題和一個詳細的字符串。Notification一般表示爲對一個動作的請求或引起用戶的注意,所以,當用戶點擊Notification項目時你可以指定一個PendingIntent來觸發。
  
  下面的代碼片段使用了setLastestEventInfo來設置這些值:
  
  Context context = getApplicationContext();
  
  // Text to display in the extended status window
  String expandedText = “Extended status text”;
  
  // Title for the expanded status
  String expandedTitle = “Notification Title”;
  
  // Intent to launch an activity when the extended text is clicked
  Intent intent = new Intent(this, MyActivity.class);
  PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);
  notification.setLatestEventInfo(context,
expandedTitle,
expandedText,
launchIntent);
  
  一個好的形式是顯示相同事件(例如,接 收多個SMS消息)的多個對象時 使用一個Notification圖 標。爲了呈現給用戶,使用setLastestEventInfo更新數據集來呈現最近的消息以及重新觸發Notification來更新它的值。
  
  你還可以使用number屬性來顯示一個狀態條圖標所表示的事件數目。
  
  設置爲比1大的數,如下所示,將在狀態條圖標上以一個小小的數字進行 顯示:
  
  notification.number++;
  
  任何對Notification的變更,你都需要重新觸發來進行更 新。如果要刪除這個數字,設置number的值爲0或者-1。
  
  最後,你可以對Notification對象使用多種屬性來增強Notification的效果,如閃爍LED、震動電話和播放音樂文件。這些高級特徵將在本章的後面部分進行詳細描述。
  
  觸發Notification
  
  爲了觸發一個Notification,使用NotificationManager的notify方法,將一個整數的ID和Notification對象傳入,如下的片段所示:
  
  int notificationRef = 1;
  notificationManager.notify(notificationRef, notification);
  
  爲了更新一個已經觸發過的Notification,傳入相同的ID。你既可以傳入相同的Notification對象,也可以是一個全新的對象。只 要ID相同,新的Notification對象會替換狀態條
圖標和擴展的狀態窗口的細節。
  
  你還可以使用ID來取消Notification,通過調用NotificationManager的cancel方法,如下所示:
  
  notificationManager.cancel(notificationRef);
  
  取消一個Notification時,將移除它的狀態條圖標以及清除 在擴展的狀態窗口中的信息。


Notification和NotificationManager的基本使用方法

1. NotificationManager和Notification用來設置通知。

通知的設置等操作相對比較簡單,基本的使用方式就是用新建一個Notification對象,然後設置好通知的各項參數,然後使用系統後臺運行的 NotificationManager服務將通知發出來。

基本步驟如下:

1)得到NotificationManager:

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);



2)創建一個新的Notification對象:

Notification notification = new Notification();

notification.icon = R.drawable.notification_icon;



也可以使用稍微複雜一些的方式創建Notification:

int icon = R.drawable.notification_icon; //通知圖標

CharSequence tickerText = "Hello";  //狀態欄(Status Bar)顯示的通知文本提示

long when = System.currentTimeMillis(); //通知產生的時間,會在通知信息裏顯示

Notification notification = new Notification(icon, tickerText, when);



3)填充Notification的各個屬性:

Context context = getApplicationContext();

CharSequence contentTitle = "My notification";

CharSequence contentText = "Hello World!";

Intent notificationIntent = new Intent(this, MyClass.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);



Notification提供了豐富的手機提示方式:

a)在狀態欄(Status Bar)顯示的通知文本提示,如:

notification.tickerText = "hello";



b)發出提示音,如:

notification.defaults |= Notification.DEFAULT_SOUND;

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");



c)手機振動,如:

notification.defaults |= Notification.DEFAULT_VIBRATE;

long[] vibrate = {0,100,200,300};

notification.vibrate = vibrate;



d)LED燈閃爍,如:

notification.defaults |= Notification.DEFAULT_LIGHTS;

notification.ledARGB = 0xff00ff00;

notification.ledOnMS = 300;

notification.ledOffMS = 1000;

notification.flags |= Notification.FLAG_SHOW_LIGHTS;



4)發送通知:

private static final int ID_NOTIFICATION = 1;

mNotificationManager.notify(ID_NOTIFICATION, notification);



2. 通知的更新

   如果需要更新一個通知,只需要在設置好notification之後,再調用setLatestEventInfo,然後重新發送一次通知即可。



3. 自定義通知視圖

   這部分可以參考官方文檔,講的很詳細了。

AndroidSDK: docs/guide/topics/ui/notifiers/notifications.html 



參考文檔:

AndroidSDK1.5 :  docs/guide/topics/ui/notifiers/notifications.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章