jeudi 28 mai 2015

Android won't stop GPS intent

I want to ask for the GPS location, even when the app is in the background. Therefore I used a PendingIntent as follows:

final Intent intent = new Intent(this, LocationReceiver.class);
mLocationIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mLocationIntent);

It works great. However, the GPS location is still updating, even when the MainActivity is destroyed. The Intent Service won't stop. I tried to stop the Intent in the onDestroyed method of the MainActivity like this:

@Override
protected void onDestroy() {
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mLocationIntent);
}

...but this method won't get called.

How can I stop the Intent when the MainActivity is destroyed?

Aucun commentaire:

Enregistrer un commentaire