闹钟AlarmManager PendingIntent获取方法中的注意事项

在开发闹钟应用时,发现使用PendingIntent的getActivity(), getBroadcast(), getService()方法时,若将requestCode和flags设为0,会导致只有一个闹钟响应。问题在于requestCode和flags的设置。FLAG_UPDATE_CURRENT用于更新Intent,确保每次启动的是最新的实体,而FLAG_ONE_SHOT则使PendingIntent在使用一次后自动取消。requestCode通常使用变量,以便区分不同的请求。建议的PendingIntent获取方式是:PendingIntent.getXXX(this,变量,intent,FLAG_UPDATE_CURRENT)。" 113559221,10536993,CDH集群高可用配置:NameNode、YARN、HBase、Hive及Flink on YARN,"['Hadoop集群', 'CDH', 'YARN HA', 'HBase HA', 'Hive HA', 'Flink on YARN', 'Zookeeper', '集群管理']
摘要由CSDN通过智能技术生成
  • 在最近写的闹钟APP中,使用到了PendingIntent,PendingIntent的获取方法有三种:

    1. getActivity(Context context,int requestCode,Intent intent,int flags)
    2. getBroadcast(Context context,int requestCode,Intent intent,int flags)
    3. getService(Context context,int requestCode,Intent intent,int flags)
  • 一开始我不了解这些方法的时候上网查询,结果其他人写的这三个方法中第二个参数和第四个参数均为0,导致了我设置多个闹钟后只有最后一个响应,后来经过排查,才发现是这两个参数设置的原因

  • 首先介绍第四个参数int flags,官方文档这样说明:

flags:Maybe FLAG_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.

  • flags的值有以下几种:

    1. FLAG_CANCEL_CURRENT
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值