PendingIntent的基本理解

PendingIntent可以看作是對(duì)Intent的一個(gè)封裝,但它不是立刻執(zhí)行某個(gè)行為,而是滿足某些條件或觸發(fā)某些事件后才執(zhí)行指定的行為(啟動(dòng)特定Service,Activity,BrcastReceive)。

我們可以把Pending Intent交給其他程序,其他程序按照PendingIntent進(jìn)行操作。

Alarm定時(shí)器與Notification通知中都使用了PendingIntent

1.獲得PendingIntent類內(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ù)說明:

context:上下文對(duì)象。

requstCode:請(qǐng)求碼,發(fā)件人的私人請(qǐng)求代碼(當(dāng)前未使用)。

intent:請(qǐng)求意圖。用于要指明要啟動(dòng)的類以及數(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ì)象通過send()方法觸發(fā)過后,PendingIntent將自動(dòng)調(diào)用cancel()進(jìn)行銷毀,那么如果你再調(diào)用send()方法的話,系統(tǒng)將會(huì)返回一個(gè)SendIntentException。

FLAG_UPDATE_CURRENT:如果系統(tǒng)中有一個(gè)和你描述的PendingIntent對(duì)等的PendingInent,那么系統(tǒng)將使用該P(yáng)endingIntent對(duì)象,但是會(huì)使用新的Intent來更新之前PendingIntent中的Intent對(duì)象數(shù)據(jù),例如更新Intent中的Extras。

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容