Java Code Examples for android.location.LocationManager#PASSIVE_PROVIDER

The following examples show how to use android.location.LocationManager#PASSIVE_PROVIDER . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocationMonitor.java    From LibreTasks with Apache License 2.0 6 votes vote down vote up
public void init() {
  lastLocation = null;
  LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  if (lm == null) {
    Log.i("LocationService", "Could not obtain LOCATION_SERVICE from the system.");
    return;
  }
  
  SharedPreferences prefs = UIDbHelperStore.instance().db().getSharedPreferences();
  String provider = null;
  if (prefs.getBoolean(context.getString(R.string.pref_key_passive), false))
  	provider = LocationManager.PASSIVE_PROVIDER;
  else
  	provider = prefs.getString(context.getString(R.string.pref_key_provider), null);
  if (provider == null) {
      Log.i("LocationService", "No location provider set.");
      return;
  }
  
  lm.requestLocationUpdates(provider, MIN_PROVIDER_UPDATE_INTERVAL, MIN_PROVIDER_UPDATE_DISTANCE,
      locationListener);
}
 
Example 2
Source File: LocationData.java    From redalert-android with Apache License 2.0 6 votes vote down vote up
public static Location getCityLocation(String cityName, Context context) {
    // Prepare cities array
    List<City> cities = getAllCities(context);

    // Loop over cities
    for (City city : cities) {
        // Got a match?
        if (city.name.equals(cityName)) {
            // Prepare new location
            Location location = new Location(LocationManager.PASSIVE_PROVIDER);

            // Set lat & long
            location.setLatitude(city.latitude);
            location.setLongitude(city.longitude);

            // Add to list
            return location;
        }
    }

    // Fail
    return null;
}
 
Example 3
Source File: LocationLogic.java    From redalert-android with Apache License 2.0 6 votes vote down vote up
public static Location getLocation(Context context) {
    // Get shared preferences
    android.content.SharedPreferences preferences = Singleton.getSharedPreferences(context);

    // Edit preferences
    double latitude = preferences.getFloat(context.getString(R.string.latitudePref), 0);
    double longitude = preferences.getFloat(context.getString(R.string.longitudePref), 0);

    // No location stored?
    if (latitude == 0) {
        return GetLastKnownLocation(context);
    }

    // Prepare new location object
    Location location = new Location(LocationManager.PASSIVE_PROVIDER);

    // Set lat & long
    location.setLatitude(latitude);
    location.setLongitude(longitude);

    // Return it
    return location;
}
 
Example 4
Source File: LocationService.java    From GracefulMovies with Apache License 2.0 5 votes vote down vote up
private void initLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (mLocationManager == null)
        return;

    List<String> providers = mLocationManager.getProviders(true);

    String locationProvider;
    /**
     * 如果首选GPS定位,会存在这种情况,上次GPS启动采集数据在A地,本次在B地需要定位,但用户恰好在室内无
     * GPS信号,只好使用上次定位数据,就出现了地区级偏差。而网络定位则更具有实时性,在精度要求不高以及室内
     * 使用场景更多的前提下,首选网络定位
     */
    if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
        locationProvider = LocationManager.NETWORK_PROVIDER; // 首选网络定位
    } else if (providers.contains(LocationManager.GPS_PROVIDER)) {
        locationProvider = LocationManager.GPS_PROVIDER;
    } else {
        locationProvider = LocationManager.PASSIVE_PROVIDER;
    }

    if (mLocationListener != null)
        mLocationManager.requestLocationUpdates(locationProvider, 2000, 10, mLocationListener);
}
 
Example 5
Source File: AndroidLocationEngineImpl.java    From mapbox-events-android with MIT License 5 votes vote down vote up
private String getBestProvider(int priority) {
  String provider = null;
  // Pick best provider only if user has not explicitly chosen passive mode
  if (priority != LocationEngineRequest.PRIORITY_NO_POWER) {
    provider = locationManager.getBestProvider(getCriteria(priority), true);
  }
  return provider != null ? provider : LocationManager.PASSIVE_PROVIDER;
}
 
Example 6
Source File: MeasurementParser.java    From TowerCollector with Mozilla Public License 2.0 5 votes vote down vote up
protected void getAndSetLastLocation() {
    lastSavedMeasurement = MeasurementsDatabase.getInstance(MyApplication.getApplication()).getLastMeasurement();
    if (lastSavedMeasurement != null) {
        // simulate location if there is starting point (some values may not be used, but don't know which exactly)
        lastSavedLocation = new Location(LocationManager.PASSIVE_PROVIDER);
        lastSavedLocation.setLatitude(lastSavedMeasurement.getLatitude());
        lastSavedLocation.setLongitude(lastSavedMeasurement.getLongitude());
        lastSavedLocation.setAccuracy(lastSavedMeasurement.getGpsAccuracy());
        lastSavedLocation.setSpeed(lastSavedMeasurement.getGpsSpeed());
        lastSavedLocation.setBearing(lastSavedMeasurement.getGpsBearing());
        lastSavedLocation.setAltitude(lastSavedMeasurement.getGpsAltitude());
        lastSavedLocation.setTime(lastSavedMeasurement.getMeasuredAt());
    }
}
 
Example 7
Source File: PassiveProvider.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public String getName() {
    return LocationManager.PASSIVE_PROVIDER;
}
 
Example 8
Source File: DistanceFilterLocationProvider.java    From background-geolocation-android with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @param value set true to engage "aggressive", battery-consuming tracking, false for stationary-region tracking
 */
private void setPace(Boolean value) {
    if (!isStarted) {
        return;
    }

    logger.info("Setting pace: {}", value);

    Boolean wasMoving   = isMoving;
    isMoving            = value;
    isAcquiringStationaryLocation = false;
    isAcquiringSpeed    = false;
    stationaryLocation  = null;

    try {
        locationManager.removeUpdates(this);
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setHorizontalAccuracy(translateDesiredAccuracy(mConfig.getDesiredAccuracy()));
        criteria.setPowerRequirement(Criteria.POWER_HIGH);

        if (isMoving) {
            // setPace can be called while moving, after distanceFilter has been recalculated.  We don't want to re-acquire velocity in this case.
            if (!wasMoving) {
                isAcquiringSpeed = true;
            }
        } else {
            isAcquiringStationaryLocation = true;
        }

        // Temporarily turn on super-aggressive geolocation on all providers when acquiring velocity or stationary location.
        if (isAcquiringSpeed || isAcquiringStationaryLocation) {
            locationAcquisitionAttempts = 0;
            // Turn on each provider aggressively for a short period of time
            List<String> matchingProviders = locationManager.getAllProviders();
            for (String provider: matchingProviders) {
                if (provider != LocationManager.PASSIVE_PROVIDER) {
                    locationManager.requestLocationUpdates(provider, 0, 0, this);
                }
            }
        } else {
            locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), mConfig.getInterval(), scaledDistanceFilter, this);
        }
    } catch (SecurityException e) {
        logger.error("Security exception: {}", e.getMessage());
        this.handleSecurityException(e);
    }
}