Notification中PendingIntent.Flag的應用

PendingIntent是一個Intent的描述、包裝,給予了這個PendingIntent 的組件在指定的事件發生或指定的時間到達時啓動Activty、Service或者Broadcast。

根據是要啓動Activity、Service還是Broadcast分別對應一個獲取PendingIntent的方法

public static PendingIntent getActivity(Context context, int requestCode,
            Intent intent, int flags)

public static PendingIntent getBroadcast(Context context, int requestCode
            Intent intent, int flags)

public static PendingIntent getService(Context context, int requestCode,
            Intent intent, int flags)


三個函數的參數都相同,其中最後一個參數flags在文檔中是這樣解析的:


flags:  May be FLAG_ONE_SHOT,LAG_NO_CREATE,LAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as  supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.

目前爲止只提供FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT這四個flag


FLAG_ONE_SHOT:this PendingIntent can only be used once. If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.
利用 FLAG_ONE_SHOT獲取的PendingIntent只能使用一次,即使再次利用上面三個方法重新獲取,再使用PendingIntent也將失敗。


FLAG_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.
利用FLAG_NO_CREAT獲取的PendingIntent,若描述的Intent不存在則返回NULL值.


FLAG_CANCEL_CURRENT:if the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.
如果描述的PendingIntent已經存在,則在產生新的Intent之前會先取消掉當前的。你可用使用它去檢索新的Intent,如果你只是想改變Intent中的額外數據的話。通過取消先前的Intent,可用確保只有最新的實體可用啓動它。如果這一保證不是問題,考慮flag_update_current。



FLAG_UPDATE_CURRENT: if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
最經常使用的是FLAG_UPDATE_CURRENT,因爲描述的Intent有 更新的時候需要用到這個flag去更新你的描述,否則組件在下次事件發生或時間到達的時候extras永遠是第一次Intent的extras。


上面4個flag中最經常使用的是FLAG_UPDATE_CURRENT,因爲描述的Intent有 更新的時候需要用到這個flag去更新你的描述,否則組件在下次事件發生或時間到達的時候extras永遠是第一次Intent的extras。
使用 FLAG_CANCEL_CURRENT也能做到更新extras,只不過是先把前面的extras清除,另外FLAG_CANCEL_CURRENT和 FLAG_UPDATE_CURRENT的區別在於能否新new一個Intent,FLAG_UPDATE_CURRENT能夠新new一個 Intent,而FLAG_CANCEL_CURRENT則不能,只能使用第一次的Intent。

此外還需要注意參數: 
int requestCode
 : Private request code for the sender (currently not used).

PendingIntent contentIntent = PendingIntent.getActivity(context,
num, intent, PendingIntent.FLAG_UPDATE_CURRENT);

對於FLAG_UPDATE_CURRENT,如果上面的requestCode 爲常量,則對於先後出現的若干Notification,則所有對應的Intent裏面的extra被更新爲最新的,就是全部同一爲最後一次的。
相反,如果num每次不一樣,則裏面的Inent的數據沒被更新。
對於FLAG_CANCEL_CURRENT,則只響應最前面的第一條Notifiacation,後面所有的不響應....

使用請參考:http://txlong-onz.iteye.com/blog/1142737
發佈了7 篇原創文章 · 獲贊 7 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章