2

I am using Google's FusedLocationProvider API to receive location updates on user's device. I am switching between PRIORITY_HIGH_ACCURACY and PRIORITY_BALANCED_POWER_ACCURACY when device GPS is on/off.

//location manager for GPS events
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//request to FusedLocationProvider API
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
    sendLocationRequest(createLocationRequest(LocationRequest.PRIORITY_HIGH_ACCURACY, interval2, fastestInterval2));
else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
    sendLocationRequest(createLocationRequest(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY, interval, fastestInterval));

The problem is when I register to receive location updates on 3G network and Wifi & GPS turned off, then priority being set to PRIORITY_BALANCED_POWER_ACCURACY, the FusedLocationProvider asks for RESOLUTION_REQUIRED, and it is to enable Wifi. I want to receive location updates on 3G network also without wifi or GPS, i.e to 100 meters block level accuracy as stated in the API documentation.

I am using these intervals:

private long /*BALANCED_POWER_ACCURACY*/interval=3600000, fastestInterval=300000, /*HIGH_ACCURACY*/interval2=10000, fastestInterval2=5000;

These are the corresponding methods:

//creating location newRequest
private LocationRequest createLocationRequest(int priority, long interval, long fastestInterval)
{
    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setInterval(interval);
    locationRequest.setFastestInterval(fastestInterval);
    locationRequest.setPriority(priority);
    return locationRequest;
}

//sending location newRequest with needed quality of service
private void sendLocationRequest(final LocationRequest locationRequest)
{
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
            Status status = locationSettingsResult.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS: {
                    //requesting location updatesPendingResult<Status> statusPendingResult =
                            LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, CurrLocationService.this);
                    break;
                }
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                {
                    Toast.makeText(CurrLocationService.this,
                            getString(R.string.app_name)+" Error: Try using GPS/Wifi" + status.getStatusMessage() + " " + status.getStatus(), Toast.LENGTH_LONG).show();
                    stopSelf();
                    break;
                }
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                {
                    Toast.makeText(CurrLocationService.this,
                            getString(R.string.app_name)+" Error: Unable to get your location updates", Toast.LENGTH_SHORT).show();
                    stopSelf();
                    break;
                }
            }
        }
    });
}
Rishab Jaiswal
  • 349
  • 3
  • 19

0 Answers0