如何检查Wi-Fi Switch状态' ON'或OFF'即使未连接到Android上的WiFi网络
我需要开发一项功能,即使未连接到 Android 上的 WiFi 网络,也需要检查 Wi-Fi 设置状态“开启”或“关闭”!我需要显示一个弹出窗口!
public static boolean CheckWifiConnection(Context context) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService (Context.CONNECTIVITY_SERVICE);
return conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable();
}
举个例子:假设我在公交车上,我已经打开了WiFi。它未连接到 wifi 网络,因为附近没有 WiFi 路由器。那么我的应用程序如何知道 wifi 是打开或关闭的。
I need to develop a feature that is required checking Wi-Fi settings state 'ON' or 'OFF' even if not connected to a WiFi network on Android! I need to display a pop up!
public static boolean CheckWifiConnection(Context context) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService (Context.CONNECTIVITY_SERVICE);
return conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable();
}
For an example: Think I am in a bus, I have turned on the WiFi. It is not connected to a wifi network, because there is no WiFi router near by. So How can my app knows wifi is on or off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
ConnectivityManager
获取网络状态You can use
ConnectivityManager
to get network status对于这个问题我可以使用
WifiManager
。WifiManager
For this question I could use
WifiManager
.WifiManager
我认为你应该使用
ConnectivityManager
。如何检查连接类型
连接管理器
I think you should to use
ConnectivityManager
.How to check a type of connection
ConnectivityManager