How to time out GPS signal acquisition(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/86b405debd6e3915bfcd8885c2ee11db2b96e3df.我自己试过了,效果很好=)
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信号采集如何超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!