Example usage for android.location LocationManager getProviders

List of usage examples for android.location LocationManager getProviders

Introduction

In this page you can find the example usage for android.location LocationManager getProviders.

Prototype

public List<String> getProviders(Criteria criteria, boolean enabledOnly) 

Source Link

Document

Returns a list of the names of LocationProviders that satisfy the given criteria, or null if none do.

Usage

From source file:com.mobilevangelist.glass.helloworld.GetTheWeatherActivity.java

public static Location getLastLocation(Context context) {
    Location result = null;//from  w w w  . ja  v a2 s  .c o  m
    LocationManager locationManager;
    Criteria locationCriteria;
    List<String> providers;

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locationCriteria = new Criteria();
    locationCriteria.setAccuracy(Criteria.NO_REQUIREMENT);
    providers = locationManager.getProviders(locationCriteria, true);

    // Note that providers = locatoinManager.getAllProviders(); is not used because the
    // list might contain disabled providers or providers that are not allowed to be called.

    //Note that getAccuracy can return 0, indicating that there is no known accuracy.

    for (String provider : providers) {
        Location location = locationManager.getLastKnownLocation(provider);
        if (result == null) {
            result = location;
        } else if (result.getAccuracy() == 0.0) {
            if (location.getAccuracy() != 0.0) {
                result = location;
                break;
            } else {
                if (result.getAccuracy() > location.getAccuracy()) {
                    result = location;
                }
            }
        }
    }

    return result;
}

From source file:uk.ac.horizon.artcodes.fragment.ExperienceRecommendFragment.java

@Nullable
private Location getLocation() {
    if (ContextCompat.checkSelfPermission(getContext(),
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        final LocationManager locationManager = (LocationManager) getContext().getApplicationContext()
                .getSystemService(Context.LOCATION_SERVICE);
        float accuracy = Float.MAX_VALUE;
        Location location = null;
        for (String provider : locationManager.getProviders(new Criteria(), true)) {
            Location newLocation = locationManager.getLastKnownLocation(provider);
            if (newLocation != null) {
                if (newLocation.getAccuracy() < accuracy) {
                    accuracy = newLocation.getAccuracy();
                    location = newLocation;
                }//from  ww  w .j av a2s .  co m
            }
        }

        return location;
    } else {
        Log.i("location", "No location permission");
    }
    return null;
}