闹钟AlarmManager PendingIntent获取方法中的注意事项
来源:互联网 发布:淘宝清理缓存后会怎样 编辑:程序博客网 时间:2024/06/05 07:46
在最近写的闹钟APP中,使用到了PendingIntent,PendingIntent的获取方法有三种:
- getActivity(Context context,int requestCode,Intent intent,int flags)
- getBroadcast(Context context,int requestCode,Intent intent,int flags)
- 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的值有以下几种:
- FLAG_CANCEL_CURRENT:当你只想改变intent中的数据,你可以用这个来重新得到新的PendingIntent;通过取消之前的PendingIntent,这确保了只有被给予新数据的实体(entity)将能够启动它,如果这种确保无所谓的,考虑使用FLAG_UPDATE_CURRENT.
- FLAG_UPDATE_CURRENT : 当第二个参数int requestCode是一个常量的话,更新当前的Intent,否则,系统就会创建一个新的Intent;和FLAG_CANCEL_CURRENT的区别是这个参数可以创建一个新的Intent。(强烈推荐使用这个!)
- FLAG_ONE_SHOT : 使用一次后就自动取消,即使你后面再利用获取PendingIntent的三个方法也无法获取PendingIntent。
- FLAG_NO_CREATE : 若intent不存在,则返回一个值为null的pendingIntent
下面介绍第二个参数int requestCode:上面第二点提到了就不再啰嗦了。
总结:PendingIntent获取方法最好写成这样:
PendingIntent pendingIntent = PendingIntent.getXXX(this,变量,intent,FLAG_UPDATE_CURRENT);
0 0
- 闹钟AlarmManager PendingIntent获取方法中的注意事项
- android中的闹钟管理器AlarmManager&PendingIntent
- 闹钟 AlarmManager PendingIntent
- 闹钟 AlarmManager PendingIntent
- 闹钟 AlarmManager PendingIntent
- Android中AlarmManager详解以及利用PendingIntent设置闹钟
- PendingIntent AlarmManager
- Android中的PendingIntent使用注意事项
- 实现可定时响起的闹钟----PendingIntent 与 AlarmManager 的运用
- PendingIntent -AlarmManager介绍
- Android PendingIntent与AlarmManager
- AlarmManager全局定时器/闹钟
- Android--闹钟AlarmManager详解
- AlarmManager手机闹钟服务
- AlarmManager(手机闹钟服务)
- 手机闹钟服务 AlarmManager
- Android闹钟-AlarmManager
- 闹钟管理器AlarmManager(整理)
- servlet的请求方式
- Myeclipse下怎样运行servlet程序
- C#读写文件流方法
- VS/VC++出现fatal error rc1107 错误的处理办法
- android端快速集成EaseUI,跳过那些坑
- 闹钟AlarmManager PendingIntent获取方法中的注意事项
- Myeclipse下部署项目以及JSP的连接
- Matlab问答day3
- 第一行代码学习笔记---安卓学习路线图
- 大数相乘、大数相加、大数相减Java版本
- Mysql各种存储引擎的特性以及如何选择存储引擎
- ProgressDialog dismiss not attached to window manager
- 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
- 对系统自带的框架MapKit的应用