The getActiveNetworkInfo() method of ConnectivityManager returns a
NetworkInfo instance representing the first connected network
interface it can find or null if none if the interfaces are connected.
Checking if this method returns null should be enough to tell if an
internet connection is available.
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
You will also need:
in your
android manifest.
Edit:
Note that having an active network interface doesn't guarantee that a
particular networked service is available. Networks issues, server
downtime, low signal, captive portals, content filters and the like
can all prevent your app from reaching a server. For instance you
can't tell for sure if your app can reach Twitter until you receive a
valid response from the Twitter service.
getActiveNetworkInfo() shouldn't never give null. I don't know what they were thinking when they came up with that. It should give you an object always.