无法接收网络相关事件
我已注册到 ConnectivityManager.CONNECTIVITY_ACTION BroadcastReceiver,用于接收网络状态事件,但当我打开或关闭 wifi 连接时,我的 onReceive
函数没有被调用。
正如文档中提到的,这是一个粘性广播接收器,当我们注册它时,它会被触发。
但是当我注册此接收器时,我的 onReceive
函数中没有收到任何事件,可能是什么原因?
在我的清单文件中,我拥有访问 Internet/网络/Wifi 连接及其状态的所有权限。
我使用以下代码注册此意图:
registerReceiver(mNetworkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
在我的 Logcat 中,我收到此广播接收器注册的以下错误消息:
01-01 00:05:29.804: ERROR/WifiHW(1305): Unable to open connection to supplicant on "/data/system/wpa_supplicant/wlan0": Connection refused
可能是什么原因?有什么方法可以查明BroadcastReceiver是否正确注册?
谢谢。
I have registered to ConnectivityManager.CONNECTIVITY_ACTION
BroadcastReceiver, for receiving network state events, but my onReceive
function is not getting called when I turn on or off my wifi connection.
As mentioned in the docs, this is an Sticky Broadcast Receiver which gets fired when we register for it.
But I am not getting any events in my onReceive
function when I register for this receiver, what might be the cause?
In my Manifest file, I have all the permissions for accessing Internet/Network/Wifi connections and their states.
I am registering with this intent using following code:
registerReceiver(mNetworkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
In my Logcat, I get following Error Message register for this broadcast receiver:
01-01 00:05:29.804: ERROR/WifiHW(1305): Unable to open connection to supplicant on "/data/system/wpa_supplicant/wlan0": Connection refused
What might be the cause? Is there any way of finding out whether BroadcastReceiver is registered properly or not?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我有用:
代码:
并且不要忘记注册所有网络权限。
This works for me:
Code:
and do not forget registering all network permission.
清单文件进行尝试
使用
MyNetworkStateReceiver
类应扩展BroadcastReceiver
的try it using the manifest file
the class
MyNetworkStateReceiver
should extendBroadcastReceiver
看起来是一个较低级别的问题 - 谷歌搜索该错误显示多个 ROM 黑客论坛及其 wifi 故障。你在测试什么?尝试在其他设备或模拟器上进行测试。 (我假设您已尝试重新启动您的:))
另外,请确保在您的活动/服务被破坏时取消注册接收器(也许这与连接错误有关)
PS我知道您说过,但仍然仔细检查 你的清单中有
。
Looks like a lower-level issue - Googling the error reveals multiple ROM hacking forums and their wifi malfunctions. What're you testing on? Try testing on another device or an emulator. (I assume you have tried rebooting yours :) )
Also, make sure you unregister the receiver when your activity/service gets destroyed (maybe this has something to do with the connection error)
PS I know you said you do, but still double check you have
in your manifest.
您也可以尝试获取互联网、WIFI和组播锁定的权限,尝试将这些添加到权限中。
我需要将这些权限用于我构建的内部应用程序,该应用程序不仅使用 WiFi 发送和接收网络数据,还使用我可以通过 C 语言中的 SDK 过滤的任何类型的网络适配器(LAN、RADIO)(设备必须扎根)。但使用这些权限我没有遇到任何问题。
You may also try getting permissions for the Internet, WIFI and Multicast lock, try adding these to the permissions.
I am required to use these permission for an in house application I have built that uses not only the WiFi for sending and receiving network data, but any type of network adapter (LAN, RADIO) that I can filtered through the SDK in C (Device must be rooted). But using these permissions I have no trouble.