Should I use ConnectivityManager and WifiManager instead of Read_Phone_State?
-
25-10-2019 - |
Question
Okay, so everywhere on Stackoverflow people say, to check if a network connectivity is available I shall add the Permision READ_PHONE_STATE
to the manifest - which is pretty ugly to me (even though many apps in the market have that permission, this one is an overkill I think; you get the IMEI, the users phonenumber etc).
So I stumbled upon the permissions ACCESS_NETWORK_STATE
and ACCESS_WIFI_STATE
, which should give the same result. Is there a reason why nobody uses them?
Because if not, I would not need the READ_PHONE_STATE
permission, which is a little bit less scary (or do users actually don't care anyways?)
Thanks!
Solution
I have used these permissions in many of my applications. A BroadcastReceiver
can check when the network has changed states, or you can check the connection status with something like this (source):
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}