Situation: My program has parts that are heavily location based, but do to speed issues for when the location is actually needed, I don't have time to lock. The solution I have designed is to get location information during primary operation (read: main screen) so that the location is available when the specific pieces need them.
Right now I register for updates and in onLocationChanged it checks if the location is accurate enough. If it is it calls removesUpdates.
Problem: If the device is somewhere that it can not get an accurate location (or any location) the GPS doesn't shut off, the batters overheat, and the users get very upset.
Question: Is there a good clean way to check if the GPS is failing to get a lock, and tell it to give up at a certain point? I created a thread timer, but because the request is tied to onPause/onResume I do not want to create extra threads that may get all tangled up.
Further to Merlin's answer (I was just typing this lot out in notepad when it appeared) - I agree. My suggestion is:
Facing a similar problem, I decided that the best thing to do would be to decouple the whole GPS from the main activity by means of a service.
By this I mean have a service which does the
requestsLocationUpdates()andremoveLocationUpdates()and implements aLocationListener. This service provides methods which the main activity can call by means of anIBinderinterface. It also sends broadcasts to the activity which implements aBroadcastReceiverto listen to these messages.So one of the the service methods your main activity could call would be (say)
where
mLocnServis the binder interface exposed by the service.The last two arguments being those to pass as arguments to requestLocationUpdates in the service. (personally I don't think these make any difference whatsoever to when the GPS turns itself off, as far as I can see it runs all the time until
removeUpdates()is called). Anyway the first argument (timeout) should be the time you are prepared to wait for a fix of the required accuracy (argument 2).So in your service you will need a Runnable as a timer, which if it times out wil send a broadcast of type
TIMED_OUT(say) and removeUpdates to stop the GPS. In your onLocationChanged you can test the location you get against the required accuracy and if good enough, send a broadcast of typeGOT_A_FIX(say) and pass the location (lat/lon and accuracy) as extras in the broadcast, then removeUpdates to stop the GPS. (TIMED_OUTandGOT_A_FIXare just example names for enums which you can make up to distinguish between the broadcast message types)The main activity can decide what to do then in its BroadcastReceiver onReceive(), i.e whether to try again if it got a
TIMED_OUTbroadcast or what to do with the data it got from theGOT_A_FIXmessage.You'll probably also need a
mLocnServ.stopGPSin the binder so that you can shut the GPS no matter what the service is doing