android之Notification通知


這篇文章寫得不錯,值得收藏,地址:http://blog.csdn.net/chenzheng_java/article/details/6249357

 

我們在用手機的時候,如果來了短信,而我們沒有點擊查看的話,是不是在手機的最上邊的狀態欄里有一個短信的小圖標提示啊?你是不是也想實現這種功能呢?今天的Notification就是解決這個問題的。

[java:nogutter]  view plain copy
  1. package cn.com.chenzheng_java;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.net.Uri;  
  10. import android.os.Bundle;  
  11. import android.provider.MediaStore.Audio;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14.   
  15. /*** 
  16.  * @description 狀態欄通知相關 
  17.  * @author chenzheng_java 
  18.  *  
  19.  */  
  20. public class NotificationActivity extends Activity {  
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.notification);  
  25.   
  26.         Button button = (Button) findViewById(R.id.button);  
  27.         button.setOnClickListener(new View.OnClickListener() {  
  28.   
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 addNotificaction();  
  32.   
  33.             }  
  34.         });  
  35.   
  36.     }  
  37.       
  38.       
  39.       
  40.   
  41.     /** 
  42.      * 添加一個notification 
  43.      */  
  44.     private void addNotificaction() {  
  45.         NotificationManager manager = (NotificationManager) this  
  46.         .getSystemService(Context.NOTIFICATION_SERVICE);  
  47.         // 創建一個Notification  
  48.         Notification notification = new Notification();  
  49.         // 設置顯示在手機最上邊的狀態欄的圖標  
  50.         notification.icon = R.drawable.excel;  
  51.         // 當當前的notification被放到狀態欄上的時候,提示內容  
  52.         notification.tickerText = "注意了,我被扔到狀態欄了";  
  53.           
  54.         /*** 
  55.          * notification.contentIntent:一個PendingIntent對象,當用戶點擊了狀態欄上的圖標時,該Intent會被觸發 
  56.          * notification.contentView:我們可以不在狀態欄放圖標而是放一個view 
  57.          * notification.deleteIntent 當當前notification被移除時執行的intent 
  58.          * notification.vibrate 當手機震動時,震動周期設置 
  59.          */  
  60.         // 添加聲音提示  
  61.         notification.defaults=Notification.DEFAULT_SOUND;  
  62.         // audioStreamType的值必須AudioManager中的值,代表着響鈴的模式  
  63.         notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;  
  64.           
  65.         //下邊的兩個方式可以添加音樂  
  66.         //notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");   
  67.         //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");   
  68.         Intent intent = new Intent(this, Notification2Activity.class);  
  69.         PendingIntent pendingIntent = PendingIntent.getActivity(this0, intent, PendingIntent.FLAG_ONE_SHOT);  
  70.         // 點擊狀態欄的圖標出現的提示信息設置  
  71.         notification.setLatestEventInfo(this"內容提示:""我就是一個測試文件", pendingIntent);  
  72.         manager.notify(1, notification);  
  73.           
  74.     }  
  75.   
  76. }  

 

點擊按鈕時候,狀態欄會顯示:

看到了吧,狀態欄多出來一個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); 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM