这是检测 WiFi 网络连接的正确方法吗?
我正在为 Android 制作一个应用程序,需要在设备连接到 WiFi 网络时做出反应,我目前正在尝试使用 BroadcastReceiver 来监视 android.net.ConnectivityManager.CONNECTIVITY_ACTION 来实现它 - 这种方式是否正确,或者有没有更合适的方法?
I'm making an application for Android that needs to react to when the device gets connected to a WiFi network, I'm currently trying to implement it using a BroadcastReceiver to monitor android.net.ConnectivityManager.CONNECTIVITY_ACTION - is this way correct, or is there a way that would be more appropriate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Jase,您需要注册才能接收
WifiManager.NETWORK_STATE_CHANGED_ACTION
。该意图有一个额外的NetworkInfo
。检查它以找到其状态。如果状态为NetworkInfo.State.CONNECTED
,则您已连接到网络。有关实现此目的的示例代码,请查看此处。
@Jase, You need to register to receive
WifiManager.NETWORK_STATE_CHANGED_ACTION
. The intent has aNetworkInfo
extra. Examine it to find its state. If the state isNetworkInfo.State.CONNECTED
, then you are connected to a network.For sample code on achieving this, look here.
看看 WifiManager.WIFI_STATE_CHANGED_ACTION
和 WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION
从广播中读取或在收到其中任何一个时进行检查,例如:
现在您可以检查状态,它将是
Take a look at WifiManager.WIFI_STATE_CHANGED_ACTION
and WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION
Read from the broadcast or do a check when you get any of them like:
Now you can check state, it will be one of