如何超时 GPS 信号采集 [英] How to time out GPS signal acquisition

查看:25
本文介绍了如何超时 GPS 信号采集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用 LocationListener 获取一个定位,然后每分钟 removeUpdates() 一次(以节省电池).问题是,如果用户进入内部,它会一直在寻找信号,从而消耗更多电池.我想知道是否有人知道位置管理器中的公共方法,如果在 15 秒内未获取信号,我可以使用它来删除更新.然后我会每五分钟检查一次定位,直到用户移回户外.这有意义吗?也许有更好的方法来超时 GPS 信号采集?我已经查看了位置管理器的公共方法,但似乎找不到方法来做到这一点.非常感谢你的帮助!-Dom

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

推荐答案

虽然 GPSmaster 的回答被接受,但我想发布更优雅和更简单的解决方案的链接,我认为 - https://gist.github.com/777790/86b405debd6e3915bfcd8885c2ee11db2df.e我自己试过了,它奏效了 =)

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 =)

如果链接不起作用,这是 amay077 的自定义 LocationListener:

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

这篇关于如何超时 GPS 信号采集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆