GPS remains on after closing Activity

4

I'm using it in my application, GPS and Google Direction. It's working, but I've noticed the following. After I get back from the activity where the map is, the GPS icon on the smartphone does not come out, it looks like it's still consuming the activity. It only comes out when I actually close the whole application.

The screen itself only traces the user's route from point A to point B.

Any tips?

    
asked by anonymous 18.07.2017 / 02:04

2 answers

3

Whatever method you are using to track the device's location, you must start it in the onResume() / onStart() method and terminate it in the onPause() / onStop() method.

If you are using FusedLocationClient it will be something like this:

@Override
public void onResume() {
    super.onResume();
    if (checkPermissions()) {
        startLocationUpdates();
    } else if (!checkPermissions()) {
        //Não implementado, apenas necessário se targetSdkVersion >= 23
        requestPermissions();
    }
}

@Override
protected void onPause() {
    super.onPause();
    mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}

See this answer for the How to get the current location of the android device? for an implementation example.

    
18.07.2017 / 15:11
2

To stop the service I do it differently from yours. I do with

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, context);

onPause (), and onStop () I do

mGoogleApiClient.disconnect();
    
18.07.2017 / 15:20