使用PendingIntent.getBrocast() 在 onReceive 中接受不到intent 的问题
来源:互联网 发布:网络舆论战 编辑:程序博客网 时间:2024/04/26 04:14
今天在service 中使用了 PendingIntent.getBrocast()这个方法去发送一个广播,然后在onReceiver中接受并获取intent中的数据。之前是这样写的:
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0,i,PendingIntent.FLAG_UPDATE_CURRENT);alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,tiggerAtTime,pendingIntent);
然后直接在onReceive中使用 intent.getStringArrayListExtra() 。但是这样做得到的ArrayList是一个空值。报错java.lang.NullPointException
查看了API文档onReceive这个方法,里面有这样一句话:
The Intent filters used in registerReceiver(BroadcastReceiver, IntentFilter)
and in application manifests are not guaranteed to be exclusive. They are hints to the operating system about how to find suitable recipients. It is possible for senders to force delivery to specific recipients, bypassing filter resolution. For this reason, onReceive()
implementations should respond only to known actions, ignoring any unexpected Intents that they may receive.
可以看到onReceiver说明了:onReceive() implementations should respond only to known actions, ignoring any unexpected Intents that they may receive. 而我在代码里没有给intent指定action,所以在onReceive方法中无法得到我想传递的intent,因此得到的数据也是空值。
这样一看,解决方法就很简单了,只需要给intent添加一个action:
i.setAction(action);
然后再onReceive方法中 判断 intent.getAction().equals(action)即可。
最后别忘了在AndroidManifest中注册你的action。
- 使用PendingIntent.getBrocast() 在 onReceive 中接受不到intent 的问题
- Notification 中PendingIntent 只打开最后一个Intent的问题
- PendingIntent不同Intent数据的覆盖问题
- 如何设置 Notification 中PendingIntent 的 Intent
- 如何设置 Notification 中PendingIntent 的 Intent
- andoid中pendingIntent和Intent的区别
- Android中intent与pendingIntent的区别
- 如何设置 Notification 中PendingIntent 的 Intent
- Android中Intent和PendingIntent的区别
- Intent和PendingIntent的使用和区别
- PendingIntent 传 intent 问题
- PendingIntent 传 intent 问题
- activity onActivityResult方法中拿不到intent的问题
- SpringMVC接受不到ajaxPOST参数的问题
- 在notification使用intent时,PendingIntent只是第一次起作用的解决方法
- Intent、PendingIntent的区别
- PendingIntent发送广播后OnReceive
- 在struts2中,接受ajax传的json对象集合,接收不到
- 51nod 1624 取余最长路
- Mac/Eclipise环境下调试Postgresql代码
- oracle 函数
- nil Nil NSNull NULL
- 数据库语言---SQL基本用法2(查询)
- 使用PendingIntent.getBrocast() 在 onReceive 中接受不到intent 的问题
- 变态跳台阶问题
- Android文件存储的问题:ClassLoader和实现Parcelable接口后 详解及用途
- 角点检测,输出坐标
- servlet 常用的api
- TCP基础知识:(一)TCP拥塞控制
- Webview展示网页,超链接使用默认浏览器打开
- WebView常见问题汇总以及解决方案
- 白盒测试中几种覆盖的区别