android FLAG_CANCEL_CURRENT FLAG_UPDATE_CURRENT

需要用到这个参数来做一个alarm提醒,因为可能会出现很多提醒,还要能取消提醒,因此遇到了这两个参数使用问题,


源码解释如下:

    /**
     * Flag for use with {@link #getActivity}, {@link #getBroadcast}, and
     * {@link #getService}: 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
     * {@link #FLAG_UPDATE_CURRENT}.
     */
    public static final int FLAG_CANCEL_CURRENT = 1<<28;
    /**
     * Flag for use with {@link #getActivity}, {@link #getBroadcast}, and
     * {@link #getService}: 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.
     */
    public static final int FLAG_UPDATE_CURRENT = 1<<27;

其实大概说明了如何使用,在这里再做点说明:

PendingIntent contentIntent = PendingIntent.getActivity(context,   
num, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
对于上述一行代码,如果num为常数,如果使用FLAG_UPDATE_CURRENT那么多个 对应的Intent里面的extra被更新为最新的,也就 是全部同一为最后一次的。

如果使用FLAG_CANCEL_CURRENT,则将取消掉对应于那个num的pendingIntet,但是对于别的num的pendingIntent并不影响。


你可能感兴趣的:(android)