0

I'm building an app that needs to keep location-tracking even when the app is in the background. Basically I am using play services' GoogleApiClient and LocationServices.FusedLocationApi.requestLocationUpdates method to invoke an IntentService.

The following code is how I trigger location updates on the background in my activity:

private void startLocationUpdatesOnBackground() { 

    mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(1000)
                .setFastestInterval(1000);

    pendingIntent = PendingIntent.getService(this, 0, 
        new Intent(this, LocationBackgroundService.class), PendingIntent.FLAG_UPDATE_CURRENT); 

    LocationServices.FusedLocationApi.
        requestLocationUpdates(mGoogleApiClient, mLocationRequest, pendingIntent);
}

It works great.
However, when the app is terminated by the user (via the task manager) I want to stop this IntentService (called LocationBackgroundService). How can I do that?

BeFree
  • 469
  • 1
  • 7
  • 15

1 Answers1

0

Try:

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

http://developer.android.com/intl/es/training/location/receive-location-updates.html#stop-updates

  • Thank you. That would be the right call, but where should I call it from? Is there some lifecycle callback to call it from in the Activity/Application/IntentService when the app is terminated? – BeFree Apr 13 '16 at 23:16
  • it depends on where you wanted to stop it. Hook a button, service onDestroy, or wherever you want. – Uriel Golab Apr 14 '16 at 03:53
  • I still don't fully get it, sorry... So the usecase is like that: a user starts the app, in the first activity's init I start location updates on the background. When the user clicks Android's left button and destroys the process - do I have a callback for that? Something like Application.onDestroy? Because that's when I would want to removeLocationUpdates - otherwise location updates keep working even though the user removed the app altogether... – BeFree Apr 15 '16 at 06:18
  • 1
    Im not an expert in Android dev, but i think you should stop receiving location in the same activity that requested them. – Uriel Golab Apr 15 '16 at 12:52