Return if location services are available - Android Map

Android examples for Map:Location

Description

Return if location services are available

Demo Code


import android.content.Context;
import android.content.ContextWrapper;
import android.location.LocationManager;
import android.util.Log;

public class Main {
  public static String TAG = "";

  /**/*  w  w  w  . j ava  2 s . c o  m*/
   * Return if location services are available
   * 
   * @param contextWrapper
   */
  public static boolean isLocationServicesEnabled(ContextWrapper contextWrapper) {
    LocationManager lm = (LocationManager) contextWrapper.getSystemService(Context.LOCATION_SERVICE);
    boolean gpsOk = false;
    boolean networkOk = false;

    try {
      if (lm.getAllProviders().contains(LocationManager.GPS_PROVIDER)) {
        gpsOk = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
      } else {
        gpsOk = true;
      }
    } catch (Exception ex) {
    }

    try {
      if (lm.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) {
        networkOk = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
      } else {
        networkOk = true;
      }
    } catch (Exception ex) {
    }

    Log.v(TAG, "GPS: " + gpsOk + ", Network:" + networkOk);

    return gpsOk && networkOk;
  }
}

Related Tutorials