Search code examples
androidgpslocationlistener

onProviderDisabled in a background service


I am using a service to track the location of the users device, they are able to toggle the service to begin tracking whenever they wish and they can toggle it off when they are done tracking.

My problem is, onProviderDisabled gets fired when the GPS is disabled, onProviderEnabled doesn't get called when the GPS is enabled again which causes me problems as I need it to switch back over to GPS when it becomes available.

Is this a known issue? Are there any workarounds?


Solution

  • If in onProviderDisabled() you call removeUpdates() for GPS listener, onProviderEnabled() would not be called for GPS adapter.

    You must requestLocationUpdates() only at service start and removeUpdates() only at service stop for each providers you want to receive locations from.