Pages

June 1, 2012

Example : Android Network connectivity check

    /**
     * Checks Internet connectivity
     * @return
     */
    public boolean isNetworkAvailable() {
           Context context = getApplicationContext();
           ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
           if (connectivity == null) {
              Log.e(tag,"network not available");
           } else {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null) {
                 for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                       return true;
                    }
                 }
              }
           }
           return false;
        }
Permission: