Android PendingIntent使用的特殊情况

PendingIntent

PendingIntent的是代表了将要发生的意图,使用的时候大概涉及到几方面:

  • SMS中在接收到短信发送状态的时候,可以设置一些回调
  • 在警报时可以用到
  • 在使用Notification时可以设置点击了提示消息后的动作,特殊的在Android 21以前notification可以设置一个deleteIntent来设置,消息被删除后的动作。在Android 21以后Android提供了notification的回调函数,这里不阐述。

    PendingIntent的flag:

    • FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
    • FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。
    • FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
    • FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。

特殊需求

在默认情况下Android系统会尽量的复用已经存在的PendingIntent,但是当我们不想复用系统中存在的PendingIntent时。我们可以使用getBroadcast(mContext, flagID, intent, PendingIntent.FLAG_UPDATE_CURRENT)的第二个参数使用不同的值就可以区分不同的PendingIntent。

ss


Intent intent = new Intent();
        intent.setAction("clear");
        intent.putExtra("id",flagID);
        PendingIntent pendingIntent= PendingIntent.getBroadcast(mContext, flagID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notify.deleteIntent = pendingIntent;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值