关于闹钟系统中,intent数据传递出错的问题修复
来源:互联网 发布:java qq好友列表 编辑:程序博客网 时间:2024/04/30 22:05
Intent intent = new Intent(context, MyReceiver.class);intent.setAction(MyReceiver.ACION_RESULT_YES);intent.putExtra("result", result);PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);am.setExact(AlarmManager.RTC_WAKEUP, remindTime, pi);闹钟设置的代码基本上是这样的,但是如果在启动的Broadcast中接收Intent过来的数据,有时会得到一个null值,也就是说,根本没有数据传过来。因此查看官方api,发现 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarm.getId(), intent, 0);的最后一个参数参数是int flag,这个值可以是FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT简单翻译一下:int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。int FLAG_UPDATE_CURRENT:如果该PendingIntent已经存在,则用新传入的Intent更新当前的数据。我们需要把最后一个参数改为PendingIntent.FLAG_UPDATE_CURRENT,这样在启动的Activity里就可以用接收Intent传送数据的方法正常接收。即:PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
1 0
- 关于闹钟系统中,intent数据传递出错的问题修复
- 关于Intent传递数据的问题(持续更新)
- intent数据传递问题
- Intent传递复杂数据的问题
- android中intent传递数据的方式
- intent 启动系统闹钟
- Intent传递的数据
- Intent的数据传递
- Intent数据的传递
- Intent的数据传递
- Intent的数据传递
- Intent数据的传递
- Intent的数据传递
- 关于数据窗口中参数设置出错的问题
- 【Android】intent传递数据时,数据大小的限制问题
- 【Android】intent传递数据时,数据大小的限制问题
- Android中Intent数据传递
- android中Intent数据传递
- Java 条件语句之 if
- 蓉蓉
- (6) HTTP接口
- java学习-基础(4)
- 【Git 教程(2)】 git工作流程
- 关于闹钟系统中,intent数据传递出错的问题修复
- Eclipse 4.6 Neon, could not create the java virtual machine
- BigDecimal精度运算、精度保存
- Virtualbox下使用virt-p2v
- Java异步调用模式
- SSH的使用和概要
- 树莓派 Qt5.7交叉编译
- 噢噢
- Context 内存泄漏