Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to time out GPS signal acquisition

My app uses the LocationListener to get one position fix and then removeUpdates() once every minute (to conserve battery). The problem is that if a user moves inside then it will perpetually look for a signal, burning more battery. I was wondering if anyone knows of a public method within Location Manager I can use to removeUpdates if a signal isn't acquired within 15 seconds. I would then check for a position fix every five minutes until the user moves back outdoors. Does this make sense? Maybe there is a better method to timeout GPS signal acquisition? I have looked through the Location Manager public methods but I can't seem to find a way to do it. Thanks so much for your help! -Dom

like image 493
GPSmaster Avatar asked Feb 02 '23 19:02

GPSmaster


1 Answers

Although GPSmaster's answer is accepted, I want to post the link to more elegant and simpler solution, I think - https://gist.github.com/777790/86b405debd6e3915bfcd8885c2ee11db2b96e3df. I have tried it myself and it worked =)

In case the link doesn't work, this is a custom LocationListener by amay077:

/**
 * Initialize instance.
 *
 * @param locaMan the base of LocationManager, can't set null.
 * @param timeOutMS timeout elapsed (mili seconds)
 * @param timeoutListener if timeout, call onTimeouted method of this.
 */
public TimeoutableLocationListener(LocationManager locaMan, long timeOutMS,
        final TimeoutLisener timeoutListener) {
    this.locaMan  = locaMan;
    timerTimeout.schedule(new TimerTask() {

        @Override
        public void run() {
            if (timeoutListener != null) {
                timeoutListener.onTimeouted(TimeoutableLocationListener.this);
            }
            stopLocationUpdateAndTimer();
        }
    }, timeOutMS);
}

/***
 * Location callback.
 *
 * If override on your concrete class, must call base.onLocation().
 */
@Override
public void onLocationChanged(Location location) {
    stopLocationUpdateAndTimer();
}

@Override
public void onProviderDisabled(String s) { }

@Override
public void onProviderEnabled(String s) { }

@Override
public void onStatusChanged(String s, int i, Bundle bundle) { }

private void stopLocationUpdateAndTimer() {
    locaMan.removeUpdates(this);

    timerTimeout.cancel();
    timerTimeout.purge();
    timerTimeout = null;
}

public interface TimeoutLisener {
    void onTimeouted(LocationListener sender);
}
like image 164
lomza Avatar answered Feb 06 '23 15:02

lomza