IntelliJ Java IDE
The moose likes Android and the fly likes Controlling location update interval Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Mobile » Android
Reply Bookmark "Controlling location update interval" Watch "Controlling location update interval" New topic
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
 
Threads others viewed
Execute Servlet At Given Time Instance
need help to move a ball from scratch
Window (Game) not registering mouse clicks occasionally
Need to restart timer with different time
Notification sending in my project using JSP
Two Laptop Bag

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture