Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

android - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...