標籤:top ica logs 系統服務 live 標題 manage 處理 靜態
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和當前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才幹夠存在。
//////////////////////////////////////////////////////////////////////////////////////////////
本文主要介紹PendingIntent的作用和舉例以及和Intent的差別。本文中代碼見[email protected]。
1、PendingIntent作用
依據字面意思就知道是延遲的intent。主要用來在某個事件完畢後運行特定的Action。PendingIntent包括了Intent及Context,所以就算Intent所屬程式結束,PendingIntent依舊有效,能夠在其它程式中使用。
經常使用在通知欄及簡訊發送系統中。
PendingIntent一般作為參數傳給某個執行個體,在該執行個體完畢某個操作後自己主動運行PendingIntent上的Action。也能夠通過PendingIntent的send函數手動運行,並能夠在send函數中設定OnFinished表示send成功後啟動並執行動作。
2、PendingIntent舉例
a. 系統通知欄
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);int icon = android.R.drawable.stat_notify_chat;long when = System.currentTimeMillis() + 2000;Notification n = new Notification(icon, "通知欄demo提醒", when);n.defaults = Notification.DEFAULT_SOUND;n.flags |= Notification.FLAG_AUTO_CANCEL;Intent openintent = new Intent(this, DemoList.class);PendingIntent pi = PendingIntent.getActivity(this, 0, openintent, PendingIntent.FLAG_CANCEL_CURRENT);n.setLatestEventInfo(this, "通知欄demo提醒title", "通知欄demo提醒text", pi);nm.notify(0, n);
setLatestEventInfo表示設定點擊該通知的事件
b. 簡訊系統舉例
簡訊系統舉例代碼
以上的兩個PendingIntent sentPI和deliveredPI將分別在簡訊發送成功和對方接受成功時被廣播
3、Intent和PendingIntent的差別
a. Intent是馬上使用的,而PendingIntent能夠等到事件發生後觸發,PendingIntent能夠cancel
b. Intent在程式結束後即終止,而PendingIntent在程式結束後依舊有效
c. PendingIntent內建Context,而Intent須要在某個Context內執行
d. Intent在原task中執行,PendingIntent在新的task中執行
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PendingIntent用於描寫敘述Intent及其終於的行為.
你能夠通過getActivity(Context context, int requestCode, Intent intent, int flags)系列方法從系統取得一個用於啟動一個Activity的PendingIntent對象,
能夠通過getService(Context context, int requestCode, Intent intent, int flags)方法從系統取得一個用於啟動一個Service的PendingIntent對象
能夠通過getBroadcast(Context context, int requestCode, Intent intent, int flags)方法從系統取得一個用於向BroadcastReceiver的Intent廣播的PendingIntent對象
返回的PendingIntent能夠遞交給別的應用程式,然後繼續處理。
這裡的話你能夠稍後才處理PendingIntent中描寫敘述的Intent及其終於行為。
當你把PendingIntent遞交給別的程式進行處理時,PendingIntent仍然擁有PendingIntent原程式所擁有的許可權(with the same permissions and identity).當你從系統取得一個PendingIntent時,一定要很小心才行。
比方,通常,假設Intent目的地是你自己的component(Activity/Service/BroadcastReceiver)的話,你最好採用在Intent中顯示指定目的component名字的方式,以確保Intent終於能發到目的。否則Intent最後可能不知道發到哪裡了。一個PendingIntent就是Android系統中的一個token(節點,這個應該是Linux或C\C++用語)的一個對象引用。它描寫敘述了一些將用於retrieve的資料(這裡,這些資料描寫敘述了Intent及其終於的行為)。
這就意味著即使PendingIntent原進程結束了的話, PendingIntent本身仍然還存在,可在其它進程(PendingIntent被遞交到的其它程式)中繼續使用.假設我在從系統中提取一個PendingIntent的,而系統中有一個和你描寫敘述的PendingIntent對等的PendingInent, 那麼系統會直接返回和該PendingIntent事實上是同一token的PendingIntent。而不是一個新的token和PendingIntent。然而你在從提取PendingIntent時,通過FLAG_CANCEL_CURRENT參數。讓這個老PendingIntent的先cancel()掉,這樣得到的pendingInten和其token的就是新的了。
通過FLAG_UPDATE_CURRENT參數的話,能夠讓新的Intent會更新之前PendingIntent中的Intent對象資料,比如更新Intent中的Extras。另外,我們也能夠在PendingIntent的原進程中調用PendingIntent的cancel ()把其從系統中移除掉。
Android pendingInten 使用方法具體解釋