Check network connection availability on android using Java

public boolean checkTheNetworkAvailability() {
 Context mycontext = getApplicationContext();
 ConnectivityManager myconnectivity = (ConnectivityManager)
        mycontext.getSystemService(Context.CONNECTIVITY_SERVICE);
 if (myconnectivity == null) {
	return false;
  // failed null!..
 } else {
  NetworkInfo[] myinfo = myconnectivity.getAllNetworkInfo();
  if (myinfo != null) {
   for (int i = 0; i < myinfo.length; i++) {
    if (myinfo[i].getState() == NetworkInfo.State.CONNECTED) {
     return true;
    }
   }
  }
 }
 return false;
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.