寫在前面的話####
之前自己在網上查找了幾篇相關文章,在此基礎上打算寫一篇總結性的筆記汰规。寫之前心想先看一下Google的API蜜唾,結果看了之后發(fā)現(xiàn)API已經寫的非常明確了,所以就干脆把API復制過來了冗美。
- PendingIntent可以看作是對Intent的一個封裝,但它不是立刻執(zhí)行某個行為析二,而是滿足某些條件或觸發(fā)某些事件后才執(zhí)行指定的行為粉洼。
- PendingIntent的獲取
-
PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags, Bundle options)
Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent)
Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.
-
PendingIntent getService (Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will start a service, like calling Context.startService()
The start arguments given to the service will come from the extras of the Intent.
-
PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast()
For security reasons, the Intent you supply here should almost always be an explicit intent, that is specify an explicit component to be delivered to through Intent.setClass
- PendingIntent的參數(shù)說明
拿第三種方式,廣播的形式說明下
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0,sIntent, 0);
第一個參數(shù)是上下文.
第二個參數(shù)是每次requestcode不同,就能產生多個Pendingintent.
第三個參數(shù)是用來存儲信息.
第四個參數(shù)是對不同操作作標識.
getBroadcast(Context context, int requestCode, Intent intent, int flags)中的flags有幾種狀態(tài):
- FLAG_CANCEL_CURRENT:如果AlarmManager管理的PendingIntent已經存在,那么將會取消當前的PendingIntent叶摄,從而創(chuàng)建一個新的PendingIntent.
- FLAG_UPDATE_CURRENT:如果AlarmManager管理的PendingIntent已經存在,讓新的Intent更新之前Intent對象數(shù)據,例如更新Intent中的Extras,另外,我們也可以在PendingIntent的原進程中調用PendingIntent的cancel ()把其從系統(tǒng)中移除掉
- FLAG_NO_CREATE:如果AlarmManager管理的PendingIntent已經存在,那么將不進行任何操作,直接返回已經.
- FLAG_ONE_SHOT:該PendingIntent只作用一次.在該PendingIntent對象通過send()方法觸發(fā)過后,PendingIntent將自動調用cancel()進行銷毀,那么如果你再調用send()方法的話,系統(tǒng)將會返回一個SendIntentException.