Android - Detect whether there is an Internet connection available?

Following method return true if there is any internet connection available and can connect to internet, and it will return if app cannot connect to internet. You must import following classes.


import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.content.Context;

And here is the method,


public boolean checkInternet() {
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
return false;
} else{
return true;
}
}

Comments

Popular posts from this blog

Fatal: LoadModule: error loading module 'mod_sql_mysql.c'

How to hide Android Soft Keyboard