I have a Location service that runs in the background as long as the the device is booted. I requested the network and g开发者_运维技巧ps provider update by calling LocationManager requestLocationUpdate method, however, when the My Location setting page is changed by enabling or disabling the location service, the onProviderEnabled or on ProviderDisabled are never called. So I want to know how could my background service got notified everytime the user changes the location setting? Can I do it in a receiver mode which caches the broadcast message ?
Yes, you can implement an extended BroadcastReceiver class within your service that listens for the GPS on/off broadcast. When the GPS is turned on or off though, it doesn't call the onProviderEnabled() method, it calls the GpsStatus.OnGpsStatusChanged() method.
The broadcast sent when the GPS is changed has event values:
GPS_EVENT_STARTED GPS_EVENT_STOPPED
See the BroadcastReceiver API on how to implement a Broadcast Receiver.
精彩评论