PendingIntent可以看作是對(duì)Intent的一個(gè)封裝,但它不是立刻執(zhí)行某個(gè)行為闪朱,而是滿(mǎn)足某些條件或觸發(fā)某些事件后才執(zhí)行指定的行為(啟動(dòng)特定Service,Activity钻洒,BrcastReceive)奋姿。
我們可以把Pending Intent交給其他程序,其他程序按照PendingIntent進(jìn)行操作素标。
在Alarm定時(shí)器與Notification通知中都使用了PendingIntent
1.獲得PendingIntent類(lèi)內(nèi)部靜態(tài)方法獲得PendingIntent實(shí)例:
//獲得一個(gè)用于啟動(dòng)特定Activity的PendingIntent
public static?PendingIntent?getActivity(Context?context, int requestCode,Intent?intent, int flags)
//獲得一個(gè)用于啟動(dòng)特定Service的PendingIntent
public static?PendingIntent?getService(Context?context, int requestCode,Intent?intent, int flags)
//獲得一個(gè)用于發(fā)送特定Broadcast的PendingIntent
public static?PendingIntent?getBroadcast(Context?context, int requestCode,Intent?intent, int flags)
參數(shù)說(shuō)明:
context:上下文對(duì)象胀蛮。
requstCode:請(qǐng)求碼,發(fā)件人的私人請(qǐng)求代碼(當(dāng)前未使用)糯钙。
intent:請(qǐng)求意圖粪狼。用于要指明要啟動(dòng)的類(lèi)以及數(shù)據(jù)的傳遞;
flags:這是一個(gè)關(guān)鍵的標(biāo)志位:
主要常量
FLAG_CANCEL_CURRENT:如果當(dāng)前系統(tǒng)中已經(jīng)存在一個(gè)相同的PendingIntent對(duì)象任岸,那么就將先將已有的PendingIntent取消再榄,然后重新生成一個(gè)PendingIntent對(duì)象。
FLAG_NO_CREATE:如果當(dāng)前系統(tǒng)中不存在相同的PendingIntent對(duì)象享潜,系統(tǒng)將不會(huì)創(chuàng)建該P(yáng)endingIntent對(duì)象而是直接返回null困鸥。
FLAG_ONE_SHOT:該P(yáng)endingIntent只作用一次。在該P(yáng)endingIntent對(duì)象通過(guò)send()方法觸發(fā)過(guò)后剑按,PendingIntent將自動(dòng)調(diào)用cancel()進(jìn)行銷(xiāo)毀疾就,那么如果你再調(diào)用send()方法的話(huà),系統(tǒng)將會(huì)返回一個(gè)SendIntentException艺蝴。
FLAG_UPDATE_CURRENT:如果系統(tǒng)中有一個(gè)和你描述的PendingIntent對(duì)等的PendingInent猬腰,那么系統(tǒng)將使用該P(yáng)endingIntent對(duì)象,但是會(huì)使用新的Intent來(lái)更新之前PendingIntent中的Intent對(duì)象數(shù)據(jù)猜敢,例如更新Intent中的Extras姑荷。