Introduction
In some Android applications, they can be run properly once the network on device is enable. Or, Android developers want to check the network status before proceed. The simple code snippet I will show today is so simple but sometime it will take you a little bit of time to find/research.
This routine was run and tested as well on Android 2.2 (API 8)
Using the code
public static boolean isNetworkAvailable(Context ctx) {
ConnectivityManager connMgr = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected() ||
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()){
return true;
}
return false;
}
History
First release on Dec 30, 2012.