Lohanry

宠辱不惊,绝不妄自菲薄

少年,你对力量一无所知


一个浪荡的程序猿

PendingIntent中Flags的参数设置

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, 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这四个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中的数据。这能确保对象被给予新的数据。如果无法保证唯一,考虑使用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存在,想继续持有他但是需要修改部分数据。这就可以被使用如果你创建了一个新的Intent当你只想修改部分数据,而且不想关注他那些以前就存在的Intent会收到新的更新数据甚至不是特别的给它的。

上面4个flag中最经常使用的是FLAG_UPDATE_CURRENT。
因为描述的Intent有 更新的时候需要用到这个flag去更新你的描述。
使用 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被更新为最新的,就是全部同一为最后一次的。
相反,如果requestCode 每次不一样,对于FLAG_CANCEL_CURRENT,则只响应最前面的第一条Notifiacation,后面所有的不响应

最近的文章

Jenkins持续集成Unity游戏项目支持多渠道多地区版

Jenkins持续集成Unity游戏项目Jenkins的安装部署和配置Jenkins中的Android打包任务设计Jenkins中的iOS打包任务设计Jenkins中的测试任务设计项目需求:Unity的游戏项目 区分大陆,台服,等不同服。大陆Android区分多渠道需要接入不同SDK和支付等。目前打包:Unity开发组完成开发后–》打出分支–》导出Unity的ios和android原生工程包–》原生项目进行各种调节并接入渠道SDK进行打包–》针对不同项目进行签名–》内部QA测试–》加固程序...…

继续阅读
更早的文章

Jfinal使用Velocity视图部署到tomcat上velocity.log (Permission denied)解决

起因:自己在搭建一个关于 EVE Online 游戏 的网站,框架用的是Jfinal的,页面模板用的是velocity,在自己开发环境上一切正常,然后部署到线上环境后出现错误。线上的环境是Ubuntu14+Nginx+Mysql+Tomcat。错误:Caused by: java.io.FileNotFoundException: velocity.log (Permission denied)定位:网上也有类似许多的问题,也有分析,我就不细说。总结起来就是: 1.使...…

继续阅读