Network availability checking in android
Obviously always the network on an Android device may not be available. We can check whether the network is currently available or not by using the below code.
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
In order to use the above code we need to add the ACCESS_NETWORK_STATE permission in the android manifest.xml file
No comments:
Post a Comment