怎么看的WiFi连接在机器人 [英] how to see if wifi is connected in android
本文介绍了怎么看的WiFi连接在机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不希望我的用户,甚至尝试下载一些东西,除非他们有wifi连接。但我只能似乎能告诉我们,如果WiFi是启用的,但他们仍然可以有3G连接。
I don't want my user to even try downloading something unless they have wifi connected. However I can only seem to be able to tell if wifi is enabled, but they could still have a 3g connection.
android.net.wifi.WifiManager m = (WifiManager) getSystemService(WIFI_SERVICE);
android.net.wifi.SupplicantState s = m.getConnectionInfo().getSupplicantState();
NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(s);
if( state != NetworkInfo.DetailedState.CONNECTED ){
return false;
}
但状态不是我所期望的,即使无线连接我收到 OBTAINING_IPADDR
的状态。
推荐答案
您应该能够使用ConnectivityManager获得WiFi适配器的状态。从那里,你可以检查是否连接,甚至可以。
You should be able to use the ConnectivityManager to get the state of the Wifi adapter. From there you can check if it is connected or even available.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}
注意:应当指出的(我们n00bies这里),你需要添加
NOTE: It should be noted (for us n00bies here) that you need to add
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
您
的AndroidManifest.xml 作为这项工作。
这篇关于怎么看的WiFi连接在机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文