I developing an app which tracks a user via GPS and reminds them if they cross a toll bridge.
I obviously need the GPS location listener to run in a service and I’ll also need a partial-wakelock so it can run occasionaly when the phone is asleep.
I also want the GPS updates to vary in frequency depending on the distance from the toll bridge to save battery.
The cwac- WakefulIntent service seems ideal for what I’m trying to achieve.
However, there are a couple of problems I can see me having before I head down this route (if you pardon the pun ;-).
Does the WakefulIntent service exit and release the wakelock once doWakefulWork() completes even if I’m waiting for my locationlistener to return some GPS updates.
How can I prevent doWakefulWork for returning until I get a location update and cleanup my listener.
What happens if I’m still waiting for a GPS update when alarm manager starts the service again, i.e. before doWakefulWork() has completed?
How can I persist data between instanciations of the service. Can I stuff an array of GPS co-ords into SharedPrefs?
Finally, as I get closer to a toll bridge I need more GPS frequent updates. Do I manage that within doWakefulWork() or by altering scheduleAlarms() so that it uses setRepeating() with a number of minutes stored in SharedPrefs by the service. The idea here is to throttle GPS usage based on proximity to an area of interest.
While the demo app provides a template to work from, I haven’t been able to find any solid examples of WakefulIntentService doing any asynchronous jobs.
Not really.
IntentServiceis not good for location tracking, because you cannot register a listener. The service will shut down onceonHandleIntent()ends.Personally, I would use
addProxmityAlert()onLocationManager, rather than mess with any of this yourself.Yes.
You don’t. You use something else, such as
LocationPoller, or, better yet,addProximityAlert().You ensure that you have appropriate timeout logic in place to prevent this, such as can be found in
LocationPoller.Yes, or a database, or a file in a format of your choosing.
You would change your alarm schedule.
WakefulIntentServiceis the “asynchronous job”. It does not execute other asynchronous jobs.