Android 中的notify 機制

大家都熟悉這樣的一個場景:就是來短信的時候,手機上方會跳出一個短信的圖標來提示你來新的信息了,然後你在上方拖下來就會看到短信息,點進去之後就能進到閱讀短信的頁面。這個流程一整套的完成就是android中的notify機制,下面我們一起來看看android中的notify機制,主要包含三個類:

   1. NotificationManager: 

                                           通知管理器,我們就理解其實一個通知消息的管理者就可以了,整個系統就一個,通過getSystemService()函數來獲得,它負責管理髮送通知、清除通知等一系列的對通知的管理工作。

    2.Notification :

                               通知本身,可以設置通知的鈴聲,震動方式等,通知點進去之後需要啓動的應用(通過PendingIntent傳遞)

    3:PendingIntent:

                              字面意思上來理解的話時懸掛起來的intent,intent的代表意圖,這個叫懸起來的意圖,解釋不通。我的理解是這樣:PendingIntent主要是爲了授予其它的應用啓動某個activity的權利,比如說:在短信程序裏面收到的消息會發出一個通知,在通知欄裏面你點擊的時候會啓動閱讀短信的那個view(一般情況下是不行的,需要通過startActiviy等方式來做,這裏是不需要的),怎麼做到的呢?就是PendingIntent做到得,因爲他在短信程序中利用PendingIntent方式賦予了再通知欄中點擊啓動短信view的權限。

下面來看代碼:

NotifytestActivity.java

  1. package com.huawei.android.notify_1;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.Button;  
  6. import android.app.NotificationManager;  
  7. public class NotifytestActivity extends Activity {  
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main2);  
  13.     }  
  14.     /** 
  15.      * 啓動該activity之後就應該清除通知標記 
  16.      */  
  17.     @Override  
  18.     protected void onStart() {  
  19.         // TODO Auto-generated method stub  
  20.         super.onStart();  
  21.         NotificationManager nNotificaitonMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  22.         nNotificaitonMan.cancel(0);  
  23.     }  
  24.       
  25. }  
NotifytestActivity2.java
  1. package com.huawei.android.notify_1;  
  2.   
  3. import android.app.Activity;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6. import android.os.Bundle;  
  7. import android.app.Notification;  
  8. import android.app.NotificationManager;  
  9. import android.app.PendingIntent;  
  10. import android.content.Intent;  
  11.   
  12. public class NotifytestActivity2 extends Activity {  
  13.   
  14.     private Button mButton1 = null;  
  15.     private Button mButton2 = null;  
  16.     //聲明消息管理器  
  17.     NotificationManager mNotifyManager = null;  
  18.     Intent mIntent = null;  
  19.     PendingIntent mPendIntent = null;  
  20.     //聲明notify對象  
  21.     Notification mNotify = null;  
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         // TODO Auto-generated method stub  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.           
  28.         //初始化notification 對象  
  29.         mNotifyManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  30.           
  31.         //獲取4個按鈕對象  
  32.         mButton1 = (Button)findViewById(R.id.btn_1);  
  33.         mButton2 = (Button)findViewById(R.id.btn_2);  
  34.         //當點擊的時候轉移內容  
  35.         mIntent = new Intent(NotifytestActivity2.this,NotifytestActivity.class);  
  36.           
  37.         //設置點擊時候顯示內容的類,  
  38.         mPendIntent = PendingIntent.getActivity(NotifytestActivity2.this,0,mIntent,0);  
  39.           
  40.         //構造notification對象  
  41.         mNotify = new Notification();  
  42.           
  43.         mButton1.setOnClickListener(new Button.OnClickListener(){  
  44.               
  45.             public void onClick(View v){  
  46.                   
  47.                 //設置通知在狀態欄顯示的圖標  
  48.                 mNotify.icon = R.drawable.icon;  
  49.                 //當我們點擊通知時顯示的內容  
  50.                 mNotify.tickerText = "來新的通知啦~~~";  
  51.                 //通知時發出的聲音  
  52.                 mNotify.defaults = Notification.DEFAULT_SOUND;  
  53.                 //設置通知顯示的參數  
  54.                 mNotify.setLatestEventInfo(NotifytestActivity2.this"Button1""Button1通知進行中", mPendIntent);  
  55.                   
  56.                 //執行這個通知事件的跳轉  
  57.                 mNotifyManager.notify(0, mNotify);  
  58.                   
  59.             }  
  60.   
  61.         });  
  62.         /** 
  63.          * 清楚通知,mNotifyManager.cancel(0)的參數0是mNotifyManager.notify(0, mNotify);裏面第一個參數,也就是notify的ID,這個在系統中是唯一的。 
  64.          * 這裏是做測試用的,在系統中應該是點擊了通知之後該通知圖標就消失了。可以看NotifytestActivity中的onStart()中的處理方式。 
  65.          */  
  66.         mButton2.setOnClickListener(new Button.OnClickListener(){  
  67.           
  68.             public void onClick(View v){  
  69.                 mNotifyManager.cancel(0);  
  70.             }  
  71.         });  
  72.           
  73.     }  
  74.   
  75.       
  76. }  

main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <Button   
  13.     android:id="@+id/btn_1"  
  14.     android:layout_width="150px"  
  15.     android:layout_height="wrap_content"  
  16.     android:text="@string/button_1"  
  17. />  
  18. <Button   
  19.     android:id="@+id/btn_2"  
  20.     android:layout_width="150px"  
  21.     android:layout_height="wrap_content"  
  22.     android:text="@string/button_2"  
  23. />  
  24. </LinearLayout>  

main2.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12.   
  13. </LinearLayout>  

運行結果如下:

1.點擊發送通知之後:



按下上方的狀態欄拖下:

3.點擊該通知後(從該通知進入到啓動的那個activity是PendingIntent的功勞哦)


關於PendingIntent的使用,下面有更詳細的解釋:

pendingIntent字面意義:等待的,未決定的Intent。
要得到一個pendingIntent對象,使用方法類的靜態方法 getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int)  分別對應着Intent的3個行爲,跳轉到一個activity組件、打開一個廣播組件和打開一個服務組件。
參數有4個,比較重要的事第三個和第一個,其次是第四個和第二個。可以看到,要得到這個對象,必須傳入一個Intent作爲參數,必須有context作爲參數。

pendingIntent是一種特殊的Intent。主要的區別在於Intent的執行立刻的,而pendingIntent的執行不是立刻的。pendingIntent執行的操作實質上是參數傳進來的Intent的操作,但是使用pendingIntent的目的在於它所包含的Intent的操作的執行是需要滿足某些條件的。
主要的使用的地方和例子:通知Notificatio的發送,短消息SmsManager的發送和警報器AlarmManager的執行等等。

Android的狀態欄通知(Notification)

如果需要查看消息,可以拖動狀態欄到屏幕下方即可查看消息。

步驟:

1 獲取通知管理器NotificationManager,它也是一個系統服務

2 建立通知Notification notification = new Notification(icon, null, when);

3 爲新通知設置參數(比如聲音,震動,燈光閃爍)

4 把新通知添加到通知管理器

發送消息的代碼如下:

//獲取通知管理器

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)

int icon = android.R.drawable.stat_notify_chat;

long when = System.currentTimeMillis();//通知發生的時間爲系統當前時間

//新建一個通知,指定其圖標和標題

Notification notification = new Notification(icon, null, when);//第一個參數爲圖標,第二個參數爲短暫提示標題,第三個爲通知時間

notification.defaults = Notification.DEFAULT_SOUND;//發出默認聲音

notification.flags |= Notification.FLAG_AUTO_CANCEL;//點擊通知後自動清除通知

Intent openintent = new Intent(this, OtherActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);//當點擊消息時就會向系統發送openintent意圖

notification.setLatestEventInfo(this, “標題”, “我是內容", contentIntent);

mNotificationManager.notify(0, notification);//第一個參數爲自定義的通知唯一標識

 

重點是setLatestEventInfo( )方法的最後一個參數!!!!它是一個PendingIntent!!!!!!!!!

這裏使用到了PendingIntent(pend本意是待定,不確定的意思)

PendingIntent可以看作是對Intent的包裝。PendingIntent主要持有的信息是它所包裝的Intent和當前ApplicationContext。正由於PendingIntent中保存有當前ApplicationContext,使它賦予帶他程序一種執行的Intent的能力,就算在執行時當前Application已經不存在了,也能通過存在PendingIntent裏的Context照樣執行Intent

 

PendingIntent的一個很好的例子:

SmsManager的用於發送短信的方法:

sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);

第一個參數:destinationAddress 對方手機號碼

第二個參數:scAddress 短信中心號碼 一般設置爲空

第三個參數:text 短信內容

第四個參數:sentIntent判斷短信是否發送成功,如果你沒有SIM卡,或者網絡中斷,則可以通過這個itent來判斷。注意強調的是“發送”的動作是否成功。那麼至於對於對方是否收到,另當別論

第五個參數:deliveryIntent當短信發送到收件人時,會收到這個deliveryIntent。即強調了“發送”後的結果

就是說是在"短信發送成功""對方收到此短信"纔會激活 sentIntentdeliveryIntent這兩個Intent。這也相當於是延遲執行了Intent


上面兩個例子可以理解,PendingIntent就是一個可以在滿足一定條件下執行的Intent,它相比於Intent的優勢在於自己攜帶有Context對象,這樣他就不必依賴於某個activity纔可以存在。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章