1

I use net code to get gps location:

    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);


    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();

    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    settingsClient.checkLocationSettings(locationSettingsRequest);

    getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    onLocationChanged(locationResult.getLastLocation());
                }
            },
            Looper.myLooper());

All work nice except that if my device have location service deactivated getFusedLocationProviderClient open a dialog box and tell that device need wifi, cell network and sensors. I agree and app activate my location service but with low power settings (battery saving mode).

What I do wrong? Look like GPS sensor is not activated because my location is displayed far from my real location. If I do manual the settings all work like a charm.

gogoloi
  • 627
  • 3
  • 8
  • 19

2 Answers2

1

Have fixed my problem!

Task<LocationSettingsResponse> result;

............

getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                onLocationChanged(locationResult.getLastLocation());
            }
        },
        Looper.myLooper());

this activate GPS too:

result = LocationServices.getSettingsClient(this).checkLocationSettings(locationSettingsRequest);
    result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
        @Override
        public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
            try {
                LocationSettingsResponse response = task.getResult(ApiException.class);
            } catch (ApiException exception) {
                switch (exception.getStatusCode()) {
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            ResolvableApiException resolvable = (ResolvableApiException) exception;
                            resolvable.startResolutionForResult(
                                    NewMainActivity.this,
                                    REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            //
                        } catch (ClassCastException e) {
                            //
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        break;
                }
            }
        }
    });
gogoloi
  • 627
  • 3
  • 8
  • 19
0

I would suggest to change the priority. The low battery system the priority should not be high. Are you running on Android Oreo? This functionality has to come under a job scheduler then. In Oreo (8.0.0+) (API 26+), How to get a location services update when the app is in the background or kill

  • I use already the hightest priority LocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); And yes I run on Oreo. – gogoloi May 26 '18 at 12:26