如何超时GPS信号捕获 [英] How to time out GPS signal acquisition
问题描述
我的应用程序使用LocationListener的获得一个位置固定,然后removeUpdates()每分钟一次(以节省电池)。的问题是,如果一个用户内移动然后它将永远寻找一个信号,燃烧更电池。我不知道是否有人知道一个公共的方法定位管理器中我可以用它来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
推荐答案
所以这就是我终于实现了。
So this is what I ended up doing.
这是我加入LocationListener的注意全局变量timertest和loccounter:
This is what i added to the LocationListener note the global variables timertest and loccounter:
public class MyLocationListener implements LocationListener
{
public void onStart() {
if (timertest==false) {
timertest=true;
serviceHandler = new Handler();
serviceHandler.postDelayed( new timer(),1000L );
}
}
class timer implements Runnable {
public void run() {
++loccounter;
if (runtest==true && loccounter>8) {
dt=200;
runtest=false;
stoplistening();
} else serviceHandler.postDelayed( this, 1000L );
}
}
}
我请求位置更新之前启动定时器权利。
I start the timer right before requesting location updates.
public void locupdate(int minTime, float minDistance) {
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
if (mlocListener != null && mlocManager != null) {
mlocManager.removeUpdates(mlocListener);
}
loccounter=0;
((MyLocationListener) mlocListener).onStart();
runtest=true;
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
minTime, minDistance, mlocListener);
}
和位置监听器内的最后一个方法:
and one last method within the location listener:
public void stoplistening() {
mlocManager.removeUpdates(mlocListener);
loccounter=0;
}
希望这可以帮助别人
Hope this helps someone
这篇关于如何超时GPS信号捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!