0
votes

In application I'm using FusedLocationProviderClient

mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

My location Request is

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

When the devices location mode is in GPS only mode, this request seems not getting any location updates.

I can make a Location settings request and ask the user th change the location mode to HighAccuracy to support the location request.

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

It will show a dialog to the user and ask him to change the location mode, But this change is permenently change the Device Location mode to High Accuracy. Even after application closed, the change remain same and consume battary.

Is there any way to change the location mode only for the application? Some thing like, When the app running it can be in HighAccuracy mode and when application closed it will go back to whatever the mode the device was before.

1

1 Answers

1
votes

you have to consider the Activity Lifecycle, in order to change the configuration to the previous state, which can be obtained by reading the configuration with the Settings Client before switching the mode & keep it for reverting later. most relevant for reverting the settings might be method onDestroy(), because it always will be the ultimate final call (unless the battery is being disconnected).

enter image description here

the Activity should implement onStart(), onResume(), onPause(), onStop(), onDestroy(). there's is even one quite suitable example in the SDK documentation:

Handling Lifecycles with Lifecycle-Aware Components.