官方文檔的解釋
A description of an Intent and target action to perform with it.
注意有一個target action,既PendingIntent是針對特定的目標活動的。
PendingIntent的實例能夠經過
getActivity(Context, int, Intent, int)
,
getActivities(Context, int, Intent[], int)
,
getBroadcast(Context, int, Intent, int)
, and
getService(Context, int, Intent, int)
;這些方法來獲得。經過這些方法獲得的PendingIntent能夠交給其餘的應用,讓其餘應用隨後來執行你所描述動做。
經過傳遞PendingIntent給其餘應用,就好像可以讓別的應用執行你指定的運算,這就好像別的應用也成了你的應用的一部分。
PendingIntent就是一個向外部應用(例如NotificationManager(通知),AlarmManager(鈴聲),桌面的AppWidgetManager)傳遞的意圖。PendingIntent容許外部應用來使用你的應用的permission來執行預先肯定的代碼。
若是你向外部應用傳遞一個intent,那個應用則傳送或者廣播你給的那個intent,他們會根據本身的permission來執行intent,可是若是你傳遞的是一個PendingIntent,你將建立你本身的permission,外部應用將會根據你的permission來執行Pendingintent中含有的intent
另外,PendingIntent是一個不會當即開始的intent。
當調用了startActivity(intent)或者StartService(intent)時,通常的intent會當即開始,而PendingIntent會在須要時纔開始。
PendingIntent在Notification中的應用實例
首先我在MainActivity中建立通知
使用一個Button來建立消息
![](http://static.javashuo.com/static/loading.gif)
運行效果
![](http://static.javashuo.com/static/loading.gif)
又例如在NfcAdapter的enableForegroundDispatch方法中,參數裏面須要加入PendingIntent
創建MainActivity 和 SecondActivity
在MainActivity中
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
當啓動MainActivity以後,將手機靠近NFC標籤纔會啓動SecondActivity
打個不恰當的比喻,能夠把PendingIntent比喻成地雷,只有當踩中這個地雷的時候,它纔會爆炸,所以只有當咱們觸發了啓動PendingIntent的動做時,纔會啓動PendingIntent內部的Intent。