android之Notification通知

iteye_21202發表於2013-05-03

我們在用手機的時候,如果來了簡訊,而我們沒有點選檢視的話,是不是在手機的最上邊的狀態列裡有一個簡訊的小圖示提示啊?你是不是也想實現這種功能呢?今天的Notification就是解決這個問題的。

  1. packagecn.com.chenzheng_java;
  2. importandroid.app.Activity;
  3. importandroid.app.Notification;
  4. importandroid.app.NotificationManager;
  5. importandroid.app.PendingIntent;
  6. importandroid.content.Context;
  7. importandroid.content.Intent;
  8. importandroid.net.Uri;
  9. importandroid.os.Bundle;
  10. importandroid.provider.MediaStore.Audio;
  11. importandroid.view.View;
  12. importandroid.widget.Button;
  13. /***
  14. *@description狀態列通知相關
  15. *@authorchenzheng_java
  16. *
  17. */
  18. publicclassNotificationActivityextendsActivity{
  19. @Override
  20. protectedvoidonCreate(BundlesavedInstanceState){
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.notification);
  23. Buttonbutton=(Button)findViewById(R.id.button);
  24. button.setOnClickListener(newView.OnClickListener(){
  25. @Override
  26. publicvoidonClick(Viewv){
  27. addNotificaction();
  28. }
  29. });
  30. }
  31. /**
  32. *新增一個notification
  33. */
  34. privatevoidaddNotificaction(){
  35. NotificationManagermanager=(NotificationManager)this
  36. .getSystemService(Context.NOTIFICATION_SERVICE);
  37. //建立一個Notification
  38. Notificationnotification=newNotification();
  39. //設定顯示在手機最上邊的狀態列的圖示
  40. notification.icon=R.drawable.excel;
  41. //噹噹前的notification被放到狀態列上的時候,提示內容
  42. notification.tickerText="注意了,我被扔到狀態列了";
  43. /***
  44. *notification.contentIntent:一個PendingIntent物件,當使用者點選了狀態列上的圖示時,該Intent會被觸發
  45. *notification.contentView:我們可以不在狀態列放圖示而是放一個view
  46. *notification.deleteIntent噹噹前notification被移除時執行的intent
  47. *notification.vibrate當手機震動時,震動週期設定
  48. */
  49. //新增聲音提示
  50. notification.defaults=Notification.DEFAULT_SOUND;
  51. //audioStreamType的值必須AudioManager中的值,代表著響鈴的模式
  52. notification.audioStreamType=android.media.AudioManager.ADJUST_LOWER;
  53. //下邊的兩個方式可以新增音樂
  54. //notification.sound=Uri.parse("file:///sdcard/notification/ringer.mp3");
  55. //notification.sound=Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI,"6");
  56. Intentintent=newIntent(this,Notification2Activity.class);
  57. PendingIntentpendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
  58. //點選狀態列的圖示出現的提示資訊設定
  59. notification.setLatestEventInfo(this,"內容提示:","我就是一個測試檔案",pendingIntent);
  60. manager.notify(1,notification);
  61. }
  62. }
package cn.com.chenzheng_java; 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.net.Uri; import android.os.Bundle; import android.provider.MediaStore.Audio; import android.view.View; import android.widget.Button; /*** * @description 狀態列通知相關 * @author chenzheng_java * */ public class NotificationActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addNotificaction(); } }); } /** * 新增一個notification */ private void addNotificaction() { NotificationManager manager = (NotificationManager) this .getSystemService(Context.NOTIFICATION_SERVICE); // 建立一個Notification Notification notification = new Notification(); // 設定顯示在手機最上邊的狀態列的圖示 notification.icon = R.drawable.excel; // 噹噹前的notification被放到狀態列上的時候,提示內容 notification.tickerText = "注意了,我被扔到狀態列了"; /*** * notification.contentIntent:一個PendingIntent物件,當使用者點選了狀態列上的圖示時,該Intent會被觸發 * notification.contentView:我們可以不在狀態列放圖示而是放一個view * notification.deleteIntent 噹噹前notification被移除時執行的intent * notification.vibrate 當手機震動時,震動週期設定 */ // 新增聲音提示 notification.defaults=Notification.DEFAULT_SOUND; // audioStreamType的值必須AudioManager中的值,代表著響鈴的模式 notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER; //下邊的兩個方式可以新增音樂 //notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); Intent intent = new Intent(this, Notification2Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); // 點選狀態列的圖示出現的提示資訊設定 notification.setLatestEventInfo(this, "內容提示:", "我就是一個測試檔案", pendingIntent); manager.notify(1, notification); } }

點選按鈕時候,狀態列會顯示:

看到了吧,狀態列多出來一個excel圖示,當我按住圖示不放,往下拖動的時候,出來了這個頁面

然後,當我們點選這個對話方塊之後,就會觸發intent,跳轉到Notification2Activity.java這個activity。

----------------------------------------------------------------------------------------

注意,NotificationManager裡的notify(id,notification)中的id是用來唯一標識我們當前的這個notification的識別符號,我們通過cancel方法刪除通知時,傳遞的就是這個值。可能讀者在看很多文件的時候,發現這個地方指定了一個莫名奇妙的值,例如R.drawable.icon,很多朋友就納悶了,為什麼這裡要指定一個圖片呢。這裡筆者就介紹下,為什麼呢?

答案其實很簡單,我們都知道,我們這裡對引數的唯一要求就是,這個id要和notify方法中的一致,並且是唯一;只要滿足了這兩項,其他的都無所謂。notify和cancel裡一致我們作為開發者,太好控制了,但是唯一呢,我們還真不好說,於是這裡就有些人動小腦筋了,很巧妙的用了我們系統中的圖片資源或者其他資源的索引ID,我們都知道,這些值肯定都是唯一的!

------------------------------------------------------------------------------------------

下面是從網上找的一些資料:

如果要新增一個Notification,可以按照以下幾個步驟

1:獲取NotificationManager:

NotificationManager m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

2:定義一個Notification:

Notification m_Notification=new Notification();

3:設定Notification的各種屬性:

//設定通知在狀態列顯示的圖示
m_Notification.icon=R.drawable.icon;

//當我們點選通知時顯示的內容
m_Notification.tickerText="Button1 通知內容.....";

通知時發出的預設聲音
m_Notification.defaults=Notification.DEFAULT_SOUND;

//設定通知顯示的引數

Intent m_Intent=new Intent(NotificationDemo.this,DesActivity.class);
PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);

m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );

//這個可以理解為開始執行這個通知
m_NotificationManager.notify(0,m_Notification);

4:既然可以增加同樣我們也可以刪除。當然是只是刪除你自己增加的。

m_NotificationManager.cancel(0);

這裡的0是一個ID號碼,和notify第一個引數0一樣。

這也就完成了,新增刪除工作。

------------------------------------------------------------------------------------------------------

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提供了豐富的手機提示方式:

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);

相關文章