PendingIntent是一個(gè)Intent的描述斤葱、包裝癞志,給予了這個(gè)PendingIntent 的組件在指定的事件發(fā)生或指定的時(shí)間到達(dá)時(shí)啟動(dòng)Activty充活、Service或者Broadcast亿眠。
根據(jù)是要啟動(dòng)Activity、Service還是Broadcast分別對(duì)應(yīng)一個(gè)獲取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)
三個(gè)函數(shù)的參數(shù)都相同会傲,其中最后一個(gè)參數(shù)flags在文檔中是這樣解析的:
flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_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.
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這四個(gè)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只能使用一次锅棕,即使再次利用上面三個(gè)方法重新獲取拙泽,再使用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已經(jīng)存在奔滑,則在使用新的Intent之前會(huì)先取消掉當(dāng)前的。你可以通過這個(gè)去取回顺少,并且通過取消先前的Intent,更新在Intent中的數(shù)據(jù)王浴。這能確保對(duì)象被給予新的數(shù)據(jù)脆炎。如果無法保證唯一,考慮使用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.
如果描述的Intent存在秒裕,想繼續(xù)持有他但是需要修改部分?jǐn)?shù)據(jù)。這就可以被使用如果你創(chuàng)建了一個(gè)新的Intent當(dāng)你只想修改部分?jǐn)?shù)據(jù)钞啸,而且不想關(guān)注他那些以前就存在的Intent會(huì)收到新的更新數(shù)據(jù)甚至不是特別的給它的几蜻。
上面4個(gè)flag中最經(jīng)常使用的是FLAG_UPDATE_CURRENT,因?yàn)槊枋龅腎ntent有 更新的時(shí)候需要用到這個(gè)flag去更新你的描述体斩。
使用 FLAG_CANCEL_CURRENT也能做到更新extras梭稚,只不過是先把前面的extras清除,另外FLAG_CANCEL_CURRENT和 FLAG_UPDATE_CURRENT的區(qū)別在于能否新new一個(gè)Intent絮吵,F(xiàn)LAG_UPDATE_CURRENT能夠新new一個(gè) Intent弧烤,而FLAG_CANCEL_CURRENT則不能,只能使用第一次的Intent蹬敲。
此外還需要注意參數(shù):
int requestCode : Private request code for the sender (currently not used).
PendingIntent contentIntent = PendingIntent.getActivity(context,
num, intent, PendingIntent.FLAG_UPDATE_CURRENT);
對(duì)于FLAG_UPDATE_CURRENT,如果上面的requestCode 為常量暇昂,則對(duì)于先后出現(xiàn)的若干Notification,則所有對(duì)應(yīng)的Intent里面的extra被更新為最新的,就是全部同一為最后一次的伴嗡。
相反急波,如果requestCode 每次不一樣,對(duì)于FLAG_CANCEL_CURRENT,則只響應(yīng)最前面的第一條Notifiacation,后面所有的不響應(yīng)