Checks device for network connectivity - Android Hardware

Android examples for Hardware:Device Feature

Description

Checks device for network connectivity

Demo Code


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class Main {
  private static final String TAG = "CPUHelper";

  /**/*  w w w .j  av  a2s .c  o m*/
   * Checks device for network connectivity
   *
   * @return If the device has data connectivity
   */
  public static boolean isNetworkAvailable(final Context c) {
    boolean state = false;
    if (c != null) {
      ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo netInfo = cm.getActiveNetworkInfo();
      if (netInfo != null && netInfo.isConnected()) {
        Log.i(TAG, "The device currently has data connectivity");
        state = true;
      } else {
        Log.i(TAG, "The device does not currently have data connectivity");
        state = false;
      }
    }
    return state;
  }
}

Related Tutorials