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?