Example usage for android.location Criteria setPowerRequirement

List of usage examples for android.location Criteria setPowerRequirement

Introduction

In this page you can find the example usage for android.location Criteria setPowerRequirement.

Prototype

public void setPowerRequirement(int level) 

Source Link

Document

Indicates the desired maximum power level.

Usage

From source file:com.mobilyzer.util.PhoneUtils.java

/**
 * Lazily initializes the location manager.
 *
 * As a side effect, assigns locationManager and locationProviderName.
 *//*from  w w w  .ja  v a  2 s  .co  m*/
private synchronized void initLocation() {
    if (locationManager == null) {
        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        Criteria criteriaCoarse = new Criteria();
        /* "Coarse" accuracy means "no need to use GPS".
         * Typically a gShots phone would be located in a building,
         * and GPS may not be able to acquire a location.
         * We only care about the location to determine the country,
         * so we don't need a super accurate location, cell/wifi is good enough.
         */
        criteriaCoarse.setAccuracy(Criteria.ACCURACY_COARSE);
        criteriaCoarse.setPowerRequirement(Criteria.POWER_LOW);
        String providerName = manager.getBestProvider(criteriaCoarse, /*enabledOnly=*/true);

        List<String> providers = manager.getAllProviders();
        for (String providerNameIter : providers) {
            try {
                LocationProvider provider = manager.getProvider(providerNameIter);
            } catch (SecurityException se) {
                // Not allowed to use this provider
                Logger.w("Unable to use provider " + providerNameIter);
                continue;
            }
            Logger.i(providerNameIter + ": "
                    + (manager.isProviderEnabled(providerNameIter) ? "enabled" : "disabled"));
        }

        /* Make sure the provider updates its location.
         * Without this, we may get a very old location, even a
         * device powercycle may not update it.
         * {@see android.location.LocationManager.getLastKnownLocation}.
         */
        manager.requestLocationUpdates(providerName, /*minTime=*/0, /*minDistance=*/0,
                new LoggingLocationListener(), Looper.getMainLooper());
        locationManager = manager;
        locationProviderName = providerName;
    }
    assert locationManager != null;
    assert locationProviderName != null;
}