| Author |
Controlling location update interval
|
Aleksandar Babic
Ranch Hand
Joined: May 30, 2007
Posts: 68
|
|
I'm developing application that should update users current location on few minute interval.
Existing built-in timeInterval option on LocationManager.requestLocationUpdates method is not reliable.
I found few solutions that solve the problem by using timer and manual control of requestLocationUpdates interval.
Is there some other solution or using timer is the best option?
|
 |
Marko Gargenta
author
Greenhorn
Joined: Mar 27, 2011
Posts: 18
|
|
|
Yes, the LocationManager takes time interval just as a hint. If you want more precise timing, I'd look into manually requesting updates when needed (plus that helps save battery). You can use AlarmService to set the intervals.
|
 |
Aleksandar Babic
Ranch Hand
Joined: May 30, 2007
Posts: 68
|
|
I never thought of using alarm service for that.
Thanks.
|
 |
Aleksandar Babic
Ranch Hand
Joined: May 30, 2007
Posts: 68
|
|
@Marko Gargenta
Still haven't tried to do it with AlarmService, but I've found some snippet that is using Handler to initiate scheduled task.
I had problem to update map when using Timer because it's not allowing to access UI from non UI thread (ERROR: Can't create handler inside thread that has not called Looper.prepare()).
Now I've created something like this in onCreate
In location listener I call removeUpdates inside onLocationChanged to stop GPS updating after retrieving location.
Now it's requesting location every 20sec (and move the marker across the map)
Is this approach good, or it has some hidden issues?
ps. Your book will certainly come to rescue
|
 |
Marko Gargenta
author
Greenhorn
Joined: Mar 27, 2011
Posts: 18
|
|
You cannot update UI thread from a non-UI thread. That's why you are getting that problem. Take a look at AsyncTask - it was designed specifically to get around this problem. It simply synchronizes threads over UI components.
I used to use Handler to post delayed messages and have something repeat over and over again that way. It works, but it's not as elegant as AlarmService.
I addressed all these issues in the book. Hopefully it helps you.
Marko
|
 |
Aleksandar Babic
Ranch Hand
Joined: May 30, 2007
Posts: 68
|
|
Thank you for a very useful answer.
I wish you all the best in your future work, and many great books.
//Aleksandar
|
 |
 |
|
|
subject: Controlling location update interval
|
|
|