Sunday, December 5, 2010

[android-developers] Re: Best approach for LocationPolling service

> > Or should I try to create a kind of service thread that polls every
> > (n) minutes and sleeps in between, would that save battery?
>
> Yes, but it will waste RAM and cause you to be the target of task killers.
I disagree - it depends on how do you implement actual "runner".

IMHO if you create location listener every X minutes and remove it
after you got a location update (10-60 seconds) - there is no wasted
RAM nor battery.
I would request updates from both providers (GPS and Network) because
Network location usually available much faster and can be used to
estimate if we need to wait the GPS or not.
Also I would implement it using Hanlder.postAtTime() approach
described at http://developer.android.com/resources/articles/timed-ui-updates.html
The Runnable should implement two things:
1. Request location updates and schedule next poll in 30 seconds. This
short interval is needed to check if we got location or not. If we
didn't get location update in 1-2 minutes then it is better to stop
updates and try it later (to preserve the battery).
2. Remove location updates and schedule next poll in 15/30 minutes

Let me know if you need more details.
Regards
Igor

On Dec 5, 1:14 pm, Mark Murphy <mmur...@commonsware.com> wrote:
> On Sun, Dec 5, 2010 at 4:02 PM, CChange <wer...@c-change.eu> wrote:
> > I am writing a little app, that needs to poll location every n minutes
> > (usually every 15 or 30 minutes), do some logic with that information
> > and store something into the db. There is a GUI to all that of course,
> > but obviously I dont want that to run in the background as well. Just
> > the polling and that little logic.
>
> > So my main question in regards to that, how would you write that if
> > you wanted to keep bat usage low.
>
> > At first I thought about the
>
> > LocationManager.addProximityAlert(..) method, but then I read that
> > that will be polled every 4 minutes which I  dont need. Or does that
> > not use additional battery because the phone notifies all listeners
> > anyway?
>
> It will only notify you when the user gets within such-and-so distance
> of a particular point, which does not seem to fit your desired
> functionality.
>
> > Or should I try to create a kind of service thread that polls every
> > (n) minutes and sleeps in between, would that save battery?
>
> Yes, but it will waste RAM and cause you to be the target of task killers.
>
> The ideal answer is to use AlarmManager, so your service only runs
> when it is actually collecting data. For GPS, though, this is tricky
> -- you do not want the GPS radio on all of the time (big battery
> drain), and there are a lot of edge cases (e.g., user has GPS
> disabled, user is in a large building and cannot get a GPS signal).
> I've been working on a WakefulLocationService that fits your desired
> functionality, but it hasn't been a high priority, so it is still in
> pieces on the lab bench.
>
> Does anyone have something like this already implemented that they could share?
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> Android App Developer Books:http://commonsware.com/books

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment