PendingIntent

创建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;
PendingIntent.getBroadcast()是Android中的一个方法,用于创建一个用于发送广播的PendingIntent对象。广播是一种用于在Android应用程序之间传递消息或事件的机制。 使用PendingIntent.getBroadcast()方法,可以创建一个待处理的意图(Intent),当该意图被触发时,系统将发送一个广播。这个广播可以被其他应用程序接收并做出相应的处理。 该方法的语法如下: ```java public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) ``` 参数说明: - context:上下文对象,一般为Activity或Service的实例; - requestCode:请求码,用于标识PendingIntent的唯一性; - intent:要发送的广播意图; - flags:标志位,用于设置PendingIntent的行为。 示例代码: ```java Intent broadcastIntent = new Intent(context, MyBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT); ``` 其中,MyBroadcastReceiver是自定义的广播接收器类,用于接收并处理广播消息。requestCode参数可根据需要进行设置,用于区分不同的PendingIntent对象。 注意:使用PendingIntent.getBroadcast()方法创建的PendingIntent对象只能用于发送广播,而不能用于启动Activity或Service。如果需要启动Activity或Service,需要使用PendingIntent.getActivity()或PendingIntent.getService()方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值