GPS still on after closing Activity

Asked

Viewed 151 times

4

I’m using in my app, GPS and Google Direction. It’s working, but I noticed the following. After I return from Activity where you have the map, the GPS icon of the smartphone does not come out, it is as if it had still consumed the activity. It only comes out when I actually close the whole app.

The screen itself (from the map) only traces the user’s route from point A to point B.

Any hint?

2 answers

3

Whatever method you use to track the location of the device should initiate it in the method onResume()/onStart() and finish it in the method onPause()/onStop().

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 reply to the question How to get current location from android device? for an example of implementation.

  • thanks for the help. I followed what you gave me and used the onPause() method. Inside it I used the LocationManager along with LocationListener which are the methods I use. I have tested and worked.

2

To stop the service I do in a different way than yours. I do with

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, context);

onPause(), and onStop() I do

mGoogleApiClient.disconnect();
  • thanks for the help. I used the onPause() method. Within it I used the LocationManager along with LocationListener which are the methods I use. I have tested and worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.