创建pendingIntent对象
个人理解是一个等待状态的意图对象,常见使用场景app 通知,
1.PendingIntent.getActivity(Context context, int requestCode,
Intent intent, @Flags int flags);
2 PendingIntent.getService(Context context, int requestCode,
@NonNull Intent intent, @Flags int flags)
3 PendingIntent.getBroadcast(Context context, int requestCode,
@NonNull Intent intent, @Flags int flags)
context 上下文对象
requestCode Private request code for the sender 发送者的请求标示
如果requestCode不相同,各个通知之间是不相关的,互不干扰
intent 即可发生的意图对象
flags 两个pendingintent 对象相同即 intent相同并且requestCode 相同时,该标志位才会有作用,
系统给定了4个标志位
/**
* Flag indicating that this PendingIntent can be used only once.
* For use with {@link #getActivity}, {@link #getBroadcast}, and
* {@link #getService}.
If set, after
* {@link #send()} is called on it, it will be automatically
* canceled for you and any future attempt to send through it will fail.
*/
这个标志表明PendingIntent只能使用一次,他和getActivity,getBroadcast,getService
一起使用。如果设置了这个标志位,调用之后,他将会自动的取消并且以后调用也会失败。
public static final int FLAG_ONE_SHOT = 1<<30;
---------------------------------------------------------------------------------------
/**
* Flag indicating that if the described PendingIntent does not
* already exist, then simply return null instead of creating it.
* For use with {@link #getActivity}, {@link #getBroadcast}, and
* {@link #getService}.
*/
这个标志表明如果所描述的pendingIntent不存在,使用getActivity
getBroadcast和getService 返回null,获取不到实例。
但是在代码中 getActivity是可以获取到pendingintent 的,getBrodcast返回的确实是null。
public static final int FLAG_NO_CREATE = 1<<29;
---------------------------------------------------------------------------------------
/**
* Flag indicating that if the described PendingIntent already exists,
* the current one should be canceled before generating a new one.
* For use with {@link #getActivity}, {@link #getBroadcast}, and
* {@link #getService}. <p>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}.
*/
这个标志位表明如果所描述的pendingIntent对象已经存在,之前的pendingintent会被取消在创建新的之前。
你可以用这个创建一个新的pendingintent 对象当你只想改变extra data时,
这样可以确保使用时拿到的是最新的数据。
如果这不是问题,可以考虑使用FLAG_UPDATE_CURRENT
public static final int FLAG_CANCEL_CURRENT = 1<<28;
---------------------------------------------------------------------------------------
/**
* Flag indicating that if the described PendingIntent already exists,
* then keep it but replace its extra data with what is in this new
* Intent. For use with {@link #getActivity}, {@link #getBroadcast}, and
* {@link #getService}. <p>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.
*/
这个标志位表明如果所描述的pendingIntent对象已经存在,他保持原来的对象,只是替换了extra data,
如果你只是想改变extras data,不关心之前的pendingIntent对象,之前的对象会加载新的数据
即使没有明确的给他赋值。
public static final int FLAG_UPDATE_CURRENT = 1<<27;