Dec 16 |
7:25 PM |
Mark M. |
has entered the room |
Mark M. |
turned on guest access |
Dec 16 |
7:40 PM |
Sean D. |
has entered the room |
Mark M. |
hello, Sean!
|
Mark M. |
how can I help you today?
|
Sean D. |
hi
|
Sean D. |
i had a quick question about using LocationClient
|
Sean D. |
i have multiple activities that need to get a location so i was wondering if there was a good way to globally access the location and reuse it across my app?
|
Dec 16 |
7:45 PM |
Sean D. |
currently i have each Activity connecting and using its own LocationClient instance
|
Mark M. |
use an event bus (LocalBroadcastManager, Otto, greenrobot's EventBus)
|
Mark M. |
send an event when you get an updated fix, to distribute it to anyone that is around that needs it
|
Sean D. |
okay so i would have a separate class that implements the LocationClient callbacks which sends the broadcast? and it's always connected?
|
Mark M. |
yes
|
Sean D. |
actually being connected doesn't drain the battery, right?
|
Mark M. |
might be a bound service, so that once all activities unbind, it shuts down
|
Sean D. |
ah okay, i didn't realize a bound service could bind to more than one Activity... that's a good idea
|
Mark M. |
regarding battery, I can't answer that, as Play Services is closed source
|
Mark M. |
and I have no idea if active clients changes its behavior
|
Mark M. |
it *probably* does
|
Sean D. |
oh
|
Sean D. |
i was thinking battery drain would just come from requesting frequent location updates (which i don't need)
|
Sean D. |
my updates would be requested every 5 mins or so
|
Dec 16 |
7:50 PM |
Mark M. |
again, I have no idea what Play Services does with a five-minute timeout
|
Mark M. |
one would hope it leverages that timeout to consume less power
|
Sean D. |
yeah i guess i'll just have to do some testing
|
Sean D. |
if it happens that it does drain the battery, i'm assuming the correct way to go would be to use a timer and reconnect every 5 mins or so?
|
Mark M. |
I'd use a ScheduledExecutorService, but, yes, that's an option
|
Sean D. |
ah okay, thanks... i haven't used timers or anything before so i wasn't sure about that
|
Sean D. |
what would be the advantage of using a bound service as opposed to just a regular class that gets created in the main activity? the automatic cleanup?
|
Mark M. |
first, I think you need a Context, and that can't be any of the Activity instances to avoid leaks
|
Mark M. |
which leaves you with Application or a Service
|
Sean D. |
ah
|
Mark M. |
you could do your own reference-counted other class too
|
Mark M. |
the problem with these chats is that I get questions from left field and have to try to answer on the fly :-)
|
Sean D. |
haha, yeah
|
Dec 16 |
7:55 PM |
Sean D. |
well thanks a lot for the advice
|
Mark M. |
you are very welcome
|
Sean D. |
see you later, probably in another several weeks... haha
|
Mark M. |
OK, sounds good
|
Dec 16 |
8:10 PM |
Sean D. |
has left the room |
Dec 16 |
8:30 PM |
Mark M. |
turned off guest access |