Android - Check for Network Connectivity

public boolean checkInternet() {
 ConnectivityManager connectivityManager = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
 if(networkInfo != null) {
  if(networkInfo.isAvailable() && networkInfo.isConnected() || networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
   Toast.makeText(getApplicationContext(), "Available", Toast.LENGTH_SHORT).show();
   return true;

  } else {
   Toast.makeText(getApplicationContext(), "Not Available", Toast.LENGTH_SHORT).show();
   return false;
  }
 }
 Toast.makeText(getApplicationContext(), "Not Available", Toast.LENGTH_SHORT).show();
 return false;
}

Comments

Popular posts from this blog

Android - Using KeyStore to encrypt and decrypt the data

Stack and Queue

Java Reflection API