For my app, I need to make sure the user is connected to wifi before contact with the server. I have found two methods to do so, but I am not sure if one suffices.
First I am adding this:
WifiManager wifiManager = (WifiManager) getActivity().getApplicationContext()
.getSystemService(WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
buildAlertNoWifi();
showProgressDialog(false, "");
return;
}
And then I am doing this:
ConnectivityManager cm = (ConnectivityManager) getActivity()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
Toast.makeText(getContext(), "Make sure you connect to wifi.", Toast.LENGTH_LONG).show();
return;
}
} else {
Toast.makeText(getContext(), "Make sure you connect to wifi.", Toast.LENGTH_LONG).show();
return;
}
So I was wondering if wifiManager.isWifiEnabled()
returns whether the device is connected to a wifi or just has wifi turned on. And if so, is it enough to use it alone?