android.net.wifi.STATE_CHANGE:Wifi 断开连接时未触发
当 Wifi 连接恢复时,仅具有操作 NETWORK_STATE_CHANGED_ACTION
(其常量值为 android.net.wifi.STATE_CHANGE
)的广播意图是否正常?即当 Wifi 断开时我没有得到这个意图。
更新:我最感兴趣的是 >= 2.2 Froyo
Is it normal to only have a broadcast intent with action NETWORK_STATE_CHANGED_ACTION
(whose constant value is android.net.wifi.STATE_CHANGE
) when a Wifi connection is coming back up? I.e. I don't get this intent when Wifi is being disconnected.
UPDATE: I am mostly interested to >= 2.2 Froyo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 android 的 API 中,它表示检查 STATE_CHANGE 的网络连接不是一个好主意,而您应该使用 SUPPLICANT_CONNECTION_CHANGE_ACTION。这将注意到 wifi 网络的建立以及 wifi 网络的断开。我不知道这是否对你有帮助,但我希望如此。 LINK
In android's API it says that it's not a good idea to check STATE_CHANGE for network connectivity and instead you should use SUPPLICANT_CONNECTION_CHANGE_ACTION. this will notice an establishment to a wifi network, and the disconnection of a wifi network. I don't know if this might help you, but I do hope so. LINK
我在我的项目中有类似的需求,最终不得不使用两者。
android.net.wifi.supplicant.CONNECTION_CHANGE 操作在网络连接时发送广播,但通常在设备具有 IP 地址之前,因此我需要 android.net.wifi.STATE_CHANGE 操作。
仅当设备与网络断开连接但 wifi 仍处于启用状态(例如,当热点超出范围时)时, android.net.wifi.STATE_CHANGE 操作才会在断开连接时接收广播,
因此您应该将接收器的两个操作放入清单:
然后添加一个 if 来检查意图中正在调用哪个操作。这是我的代码中的 BroadcastReceiver 的 onReceive 方法:
I had a similar need in my project and ended up having to use both.
The android.net.wifi.supplicant.CONNECTION_CHANGE action sends a broadcast when the network is connected, but usually before the device has an IP address, so I needed the android.net.wifi.STATE_CHANGE action for that.
The android.net.wifi.STATE_CHANGE action receives a broadcast on disconnect only if the device is disconnecting from a network, but wifi is still enabled (when hotspot goes out of range, for example)
So you should put both actions for the receiver in the manifest:
and you put an if to check which action is being called in the intent. Here is the onReceive method of the BroadcastReceiver in my code: