Here is a great helper method by MisterSquonk to detect if your app has an internet connection or not.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public boolean hasInternet(Activity a) { boolean hasConnectedWifi = false; boolean hasConnectedMobile = false; ConnectivityManager cm = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("wifi")) if (ni.isConnected()) hasConnectedWifi = true; if (ni.getTypeName().equalsIgnoreCase("mobile")) if (ni.isConnected()) hasConnectedMobile = true; } return hasConnectedWifi || hasConnectedMobile; } |
Note that this method cannot be used to check if an external site you are trying to access is alive or not.