如何确定的Andr​​oid上网吗?Andr、oid

由网友(转身、遇见鬼)分享简介:我如何能够确定当前可用的网络连接类型的Andr​​oid设备?例如有它返回的WiFi,3G,没有。How can I determine the current internet connection type available to an Android device? e.g. have it return W...

我如何能够确定当前可用的网络连接类型的Andr​​oid设备?例如有它返回的WiFi,3G,没有。

How can I determine the current internet connection type available to an Android device? e.g. have it return WiFi, 3G, none.

推荐答案

您可以用它来确定您是否连接:

You can use this to determine whether you are connected:

final ConnectivityManager connectManager = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE); // ctx stands for the Context
final NetworkInfo mobile = connectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
final NetworkInfo wifi   = connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI  );

// Return true if connected, either in 3G or wi-fi
return ((mobile != null && mobile.getState() == NetworkInfo.State.CONNECTED) || 
        (wifi   != null && wifi.getState()   == NetworkInfo.State.CONNECTED)   );
}

这code要求的权限 ACCESS_NETWORK_STATE ACCESS_WIFI_STATE

This code requires the permissions ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE.

阅读全文

相关推荐

最新文章