CQM
CQM

Reputation: 44230

Android GPS getLastKnownLocation if locationUpdater takes too long

with android and using GPS as a location provider, how can I tell it to stop looking for a GPS lock after a specified number of seconds and simply resort to using the last known location instead?

I am currently doing it the other way around, where I get the lask known location first, and then let the request update function run. My problem with this is that the GPS will sometimes NEVER get a fix, but it doesn't stop trying to get a fix. Very annoying issue to me (and probably to some users)

suggestions?

Upvotes: 0

Views: 681

Answers (2)

Sush
Sush

Reputation: 6899

May be you can use a Timer for this. you can follow below steps.

  1. Define a TimerTask that will unregister the Location listener and get the lask known location.
  2. Before registering the Location listener , define and schedule a timer with the above TimerTask and a delay say 60000 miliseconds (Your timeout period). Use the below method for schedule.

    public void schedule (TimerTask task, long delay)   
    e.g. timer1.schedule(timerTask,60000)
    

This should stop the location listener after 60 seconds as you are unregistering it in the timertask. Hope it will help.

Upvotes: 2

Jack
Jack

Reputation: 9242

When you unregister your location listener, it should stop trying to get a fix. Please see this link for a reference. I also had this issue with a recent application of mine, and unregistering the listener worked.

Upvotes: 0

Related Questions