Issue
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?
Solution
If you want to do it this way you will need to provide the user a way to turn this on and off.
If you register for GPS updates using a PendingIntent
, this means that your app will get the GPS updates even when it is not running. This is what this is designed for. Even the documentation is pretty clear about this:
This method is suited for the background use cases, more specifically for receiving location updates, even when the app has been killed by the system.
If you only want to get GPS updates while your app is running, then you should use one of the other requestLocationUpdates()
variants that use a callback or a listener. These variants will stop getting updates if your app is killed.
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.