We needed a location service for our app that was available from anyplace.
We used s singletone with Application context for system services and more, and has two basic methods start and stop which listen to location providers and stop listening respectively.
We have getLastLocation method that retrieves the current most updated and accurate location we could find.
We have another method that gives me a headache 🙂
Location getLocationWithWait(Class c) which supposed to get a location if available, if not it will return null and will create a Timer for 45 seconds.if the 45 seconds have passed and no location was found then the timer will broadcast to the class given in the parameter a ‘no location’ broadcast, otherwise if the location callbacks will be called they will cancel the timer and will send ‘location found’ broadcast to the class in the parameter.
So here are my questions:
- i started to think the whole
architecture here is wrong, any
better way to implement a location
service to cater ANY Activity in my
application ? - i used a Timer an not Handler since
the singletone i use is NOT an
activity so i have no Looper an
creating a thread with looper looks
to me just as bad as creating
another Thread for the timer… i
did wanted to use the Activity
instead, but then… if the activity
pauses and resumes i need to keep
track of the timer’s remaining time. - the broadcasts may also be of a
problem since if the activty pauses
it will miss the broadcast and then
i need to keep track in my
singletone after several states,
StickyBroadcast may solve it but
that one i can’t send to specific
class but to the whole system only.
So finally will it be any advantage to make this class a Service ?(to be honest i thought of maybe one advantage but not much) i want a location service with timeout much like google do in their “My Location” in maps.
10x.
Wouldn’t it be better to use a LocationListener, as described in Requesting Location Updates?
You can then use a ‘FastFix’ in onResume()
Without knowing a bit more about your requirements, it’s difficult to make more specific suggestions.
Hope this helps,
Phil Lello