title: How to check the network availability?
body: <p>I have to connect my app with server using either wifi (if it is available),
  or gprs (if wifi is not available). 
  Here is my code to check the connection availability</p>
  
  <pre><code>public static final boolean isConnectionAvailable(Activity a)
  {  
  
    ConnectivityManager cm = (ConnectivityManager)a.getSystemService(Context.CONNECTIVITY_SERVICE);
  
    State mobile = cm.getNetworkInfo(0).getState();
  
    State wifi = cm.getNetworkInfo(1).getState();
  
    if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
      {
          return true;
      }
    if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING)
      {
          return true;
      }
  
     return false;
  }      
  </code></pre>
  
  <p>Is this a correct way? Can anyone suggest me a better way?  </p>