zoukankan      html  css  js  c++  java
  • 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 beFLAG_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.

    目前为止只提供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_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.


    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.


    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.

    上面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。是否更新还取决于第二个参数,如果第二个

    另外两flag就比较少用,利用FLAG_ONE_SHOT获取的PendingIntent只能使用一次,即使再次利用上面三个方法重新获取,再使用PendingIntent也将失败,利用FLAG_NO_CREAT获取的PendingIntent若描述的Intent不存在则返回NULL值.

    注意当需要发送不同的pendingIntent 的时候 必须设置不同的  requestCode .否者将会出现可能多次发送的是同一个intent事件


  • 相关阅读:
    为什么不应该使用ZooKeeper做服务发现
    Python 练习 人事管理
    Python 爬取 妹子图(技术是无罪的)
    Python 爬取 书籍
    Python 爬取 豆瓣
    Python 爬取 房天下
    Python 爬取 煎蛋
    Python 爬取 拉钩
    Python 爬取 猫眼
    家具专区网
  • 原文地址:https://www.cnblogs.com/krislight1105/p/3748303.html
Copyright © 2011-2022 走看看