大家都熟悉這樣的一個場景:就是來短信的時候,手機上方會跳出一個短信的圖標來提示你來新的信息了,然後你在上方拖下來就會看到短信息,點進去之後就能進到閱讀短信的頁面。這個流程一整套的完成就是android中的notify機制,下面我們一起來看看android中的notify機制,主要包含三個類:
1. NotificationManager:
通知管理器,我們就理解其實一個通知消息的管理者就可以了,整個系統就一個,通過getSystemService()函數來獲得,它負責管理髮送通知、清除通知等一系列的對通知的管理工作。
2.Notification :
通知本身,可以設置通知的鈴聲,震動方式等,通知點進去之後需要啓動的應用(通過PendingIntent傳遞)
3:PendingIntent:
字面意思上來理解的話時懸掛起來的intent,intent的代表意圖,這個叫懸起來的意圖,解釋不通。我的理解是這樣:PendingIntent主要是爲了授予其它的應用啓動某個activity的權利,比如說:在短信程序裏面收到的消息會發出一個通知,在通知欄裏面你點擊的時候會啓動閱讀短信的那個view(一般情況下是不行的,需要通過startActiviy等方式來做,這裏是不需要的),怎麼做到的呢?就是PendingIntent做到得,因爲他在短信程序中利用PendingIntent方式賦予了再通知欄中點擊啓動短信view的權限。
下面來看代碼:
NotifytestActivity.java
- package com.huawei.android.notify_1;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Button;
- import android.app.NotificationManager;
- public class NotifytestActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main2);
- }
- /**
- * 啓動該activity之後就應該清除通知標記
- */
- @Override
- protected void onStart() {
- // TODO Auto-generated method stub
- super.onStart();
- NotificationManager nNotificaitonMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- nNotificaitonMan.cancel(0);
- }
- }
- package com.huawei.android.notify_1;
- import android.app.Activity;
- import android.view.View;
- import android.widget.Button;
- import android.os.Bundle;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- public class NotifytestActivity2 extends Activity {
- private Button mButton1 = null;
- private Button mButton2 = null;
- //聲明消息管理器
- NotificationManager mNotifyManager = null;
- Intent mIntent = null;
- PendingIntent mPendIntent = null;
- //聲明notify對象
- Notification mNotify = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //初始化notification 對象
- mNotifyManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- //獲取4個按鈕對象
- mButton1 = (Button)findViewById(R.id.btn_1);
- mButton2 = (Button)findViewById(R.id.btn_2);
- //當點擊的時候轉移內容
- mIntent = new Intent(NotifytestActivity2.this,NotifytestActivity.class);
- //設置點擊時候顯示內容的類,
- mPendIntent = PendingIntent.getActivity(NotifytestActivity2.this,0,mIntent,0);
- //構造notification對象
- mNotify = new Notification();
- mButton1.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v){
- //設置通知在狀態欄顯示的圖標
- mNotify.icon = R.drawable.icon;
- //當我們點擊通知時顯示的內容
- mNotify.tickerText = "來新的通知啦~~~";
- //通知時發出的聲音
- mNotify.defaults = Notification.DEFAULT_SOUND;
- //設置通知顯示的參數
- mNotify.setLatestEventInfo(NotifytestActivity2.this, "Button1", "Button1通知進行中", mPendIntent);
- //執行這個通知事件的跳轉
- mNotifyManager.notify(0, mNotify);
- }
- });
- /**
- * 清楚通知,mNotifyManager.cancel(0)的參數0是mNotifyManager.notify(0, mNotify);裏面第一個參數,也就是notify的ID,這個在系統中是唯一的。
- * 這裏是做測試用的,在系統中應該是點擊了通知之後該通知圖標就消失了。可以看NotifytestActivity中的onStart()中的處理方式。
- */
- mButton2.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v){
- mNotifyManager.cancel(0);
- }
- });
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/btn_1"
- android:layout_width="150px"
- android:layout_height="wrap_content"
- android:text="@string/button_1"
- />
- <Button
- android:id="@+id/btn_2"
- android:layout_width="150px"
- android:layout_height="wrap_content"
- android:text="@string/button_2"
- />
- </LinearLayout>
main2.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </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作爲參數。
主要的使用的地方和例子:通知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和當前Application的Context。正由於PendingIntent中保存有當前Application的Context,使它賦予帶他程序一種執行的Intent的能力,就算在執行時當前Application已經不存在了,也能通過存在PendingIntent裏的Context照樣執行Intent。
PendingIntent的一個很好的例子:
SmsManager的用於發送短信的方法:
sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
第一個參數:destinationAddress 對方手機號碼
第二個參數:scAddress 短信中心號碼 一般設置爲空
第三個參數:text 短信內容
第四個參數:sentIntent判斷短信是否發送成功,如果你沒有SIM卡,或者網絡中斷,則可以通過這個itent來判斷。注意強調的是“發送”的動作是否成功。那麼至於對於對方是否收到,另當別論
第五個參數:deliveryIntent當短信發送到收件人時,會收到這個deliveryIntent。即強調了“發送”後的結果
就是說是在"短信發送成功"和"對方收到此短信"纔會激活 sentIntent和deliveryIntent這兩個Intent。這也相當於是延遲執行了Intent
上面兩個例子可以理解,PendingIntent就是一個可以在滿足一定條件下執行的Intent,它相比於Intent的優勢在於自己攜帶有Context對象,這樣他就不必依賴於某個activity纔可以存在。