Is it on? An internet connection checker snippet


SDK Version: 
M3

Most of our apps use the internet in some way, so we made an util class, that we use regularly. To save some bandwidth, I only use it after a connection related exception(ioexception etc) was thrown, but of course checking ahead is a good idea sometimes.

It works like this:
If I get a connection related exception anywhere, I pass it up to the UI, where I call the helper method below.

  1. public class NetChecker {
  2.         public static boolean isOnline() {
  3.                 //ConnectivityManager is used to check available network(s)
  4.                 ConnectivityManager cm = (ConnectivityManager) YourContext().getSystemService(Context.CONNECTIVITY_SERVICE);
  5.                 if (cm.getActiveNetworkInfo() == null){
  6.                         //no network is available
  7.                         return false;
  8.                 } else {
  9.                         //at least one type of network is available
  10.                         return true;
  11.                 }
  12.         }
  13.  
  14.         public static enum NetStatus {
  15.                 NONET, FAULTYURL, NETOK, CONN_MISSING
  16.         }
  17.  
  18.         public static NetStatus checkNet() {
  19.                 // is there an active connection to any network?
  20.                 if (isOnline() == false) {
  21.                         return NetStatus.CONN_MISSING;
  22.                 }
  23.  
  24.                 // is the server reachable?
  25.                 try {
  26.                         URL url = new URL("http://helloandroid.com");
  27.  
  28.                         HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
  29.                         urlc.setConnectTimeout(1000 * 5); // Timeout is in seconds
  30.                         urlc.connect();
  31.                         if (urlc.getResponseCode() == 200) {
  32.                                 //http response is OK
  33.                                 return NetStatus.NETOK;
  34.                         }
  35.                 } catch (MalformedURLException e1) {
  36.                         return NetStatus.FAULTYURL;
  37.                 } catch (IOException e) {
  38.                         return NetStatus.NONET;
  39.                 }
  40.                 return NetStatus.NETOK;
  41.         }
  42. }

Helper class for the ui:

  1. public class NetCheckerHelper {
  2.         public static boolean netCheckerHelper(){
  3.                 switch(NetChecker.checkNet()){
  4.                 case CONN_MISSING:
  5.                         //User should enable wifi/3g etc
  6.                         return false;
  7.                 case NONET:
  8.                         //User has enabled network, but it's not working
  9.                         return false;
  10.                 case FAULTYURL:
  11.                         //Url of the server is faulty
  12.                         return false;
  13.                 case NETOK:
  14.                         //Maybe the problem has been solved since the exception was thrown. Retry the last method that threw the exception
  15.                         return true;
  16.                 }
  17.                 return false;
  18.         }
  19. }