怎么看的WiFi连接在机器人怎么看、机器人、WiFi

由网友(男人你得有范)分享简介:我不希望我的用户,甚至尝试下载一些东西,除非他们有wifi连接。但我只能似乎能告诉我们,如果WiFi是启用的,但他们仍然可以有3G连接。 I don't want my user to even try downloading something unless they have wifi connected. H...

我不希望我的用户,甚至尝试下载一些东西,除非他们有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>

的Andr​​oidManifest.xml 作为这项工作。

阅读全文

相关推荐

最新文章