All I want to do is see if the network is actually enabled (whether or not there is a check by the option in the settings menu). How do I go about doi开发者_StackOverflow社区ng this?
To be specific:
I got Wifi through WifiManager... ...and everything else has been a nightmare with everything else (specifically mobile data).
This is how I'm trying with Mobile Data and WiMax:
cm.getNetworkInfo(cm.TYPE_WIMAX).getDetailedState();
cm.getNetworkInfo(cm.TYPE_MOBILE).getDetailedState();
But those return DISCONNECTED when they are disabled or actually disconnected.
EDIT: I found it: http://developer.android.com/reference/android/provider/Settings.System.html
EDIT again: This doesn't really have what I want either... I will keep searching, though!
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
boolean isNetworkEnabled = (info == null ? false : true);
Were you thinking of implementing these two methods from LocationListener
: onProviderEnabled()
, onProviderDisabled()
?
However, I think requestLocationUpdates()
must be called first before with the listener before these methods will be called.
public static boolean checkConnection(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() && wifi.isConnected()) {
status = true;
}
if (mobile.isAvailable() && mobile.isConnected()) {
status = true;
} else {
status = false;
}
return status;
}
精彩评论