如何确定的Android上网吗?
问题描述:
我如何能够确定当前可用的网络连接类型的Android设备?例如有它返回的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
.