天天看點

Android---PendingIntent

intent英文意思是意圖,pending表示即将發生或來臨的事情。

PendingIntent這個類用于處理即将發生的事情。比如在通知Notification中用于跳轉頁面,但不是馬上跳轉。

Intent 是及時啟動,intent 随所在的activity 消失而消失。

PendingIntent 可以看作是對intent的包裝,通常通過getActivity,getBroadcast ,getService來得到pendingintent的執行個體,目前activity并不能馬上啟動它所包含的intent,而是在外部執行 pendingintent時,調用intent的。正由于pendingintent中 儲存有目前App的Context,使它賦予外部App一種能力,使得外部App可以如同目前App一樣的執行pendingintent裡的 Intent, 就算在執行時目前App已經不存在了,也能通過存在pendingintent裡的Context照樣執行Intent。另外還可以處理intent執行後的操作。常和alermanger 和notificationmanager一起使用。

Intent一般是用作Activity、Sercvice、BroadcastReceiver之間傳遞資料,而Pendingintent,一般用在 Notification上,可以了解為延遲執行的intent,PendingIntent是對Intent一個包裝。

PendingIntent就是一個Intent的描述,我們可以把這個描述交給别的程式,别的程式根據這個描述在後面的别的時間做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相當于PendingIntent代表了Intent)。

你可以通過getActivity(Context context, int requestCode, Intent intent, int flags)系列方法從系統取得一個用于啟動一個Activity的PendingIntent對象,(Private request code for the sender (currently not used).)

可以通過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 ()把其從系統中移除掉。

注意:兩個PendingIntent對等是指它們的operation一樣, 且其它們的Intent的action, data, categories, components和flags都一樣。但是它們的Intent的Extra可以不一樣。

主要常量

FLAG_CANCEL_CURRENT:如果目前系統中已經存在一個相同的PendingIntent對象,那麼就将先将已有的PendingIntent取消,然後重新生成一個PendingIntent對象。

FLAG_NO_CREATE:如果目前系統中不存在相同的PendingIntent對象,系統将不會建立該PendingIntent對象而是直接傳回null。

FLAG_ONE_SHOT:該PendingIntent隻作用一次。在該PendingIntent對象通過send()方法觸發過後,PendingIntent将自動調用cancel()進行銷毀,那麼如果你再調用send()方法的話,系統将會傳回一個SendIntentException。

FLAG_UPDATE_CURRENT:如果系統中有一個和你描述的PendingIntent對等的PendingInent,那麼系統将使用該PendingIntent對象,但是會使用新的Intent來更新之前PendingIntent中的Intent對象資料,例如更新Intent中的Extras。

因為對于Context的startActivity方法,如果不是在其子類(Activity)中調用,那麼必須對Intent加上FLAG_ACTIVITY_NEW_TASK。

繼續閱讀