设置每次 GPS 定位之间的间隔
我试图在 Android 中的每次 GPS 修复之间设置 5 分钟的间隔。我的代码如下所示:
private void startMonitoring() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocListener();
if (locationManager.isProviderEnabled(locationManager.GPS_PROVIDER)){
startUpdates();
} else {
// I open here the preferences to force the user start the GPS
}
}
private void startUpdates(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
public class LocListener implements LocationListener{
@Override
public void onLocationChanged(Location loc){
...
locationManager.removeUpdates(locationListener);
scheduleUpdates();
}
...
}
private void scheduleUpdates(){
// Wait 5 minutes
handler.sleep(5 * 60 * 1000);
startUpdates();
}
class WaitHandler extends Handler {
@Override
public void handleMessage(Message message) {}
public void sleep (long delayMillis){
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
}
我已经为此工作了几个小时,但我还没有找到一个好的解决方案。任何帮助将不胜感激,
非常感谢,
I am trying to set up a 5 minutes interval between every GPS fix in Android. My code looks like this:
private void startMonitoring() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocListener();
if (locationManager.isProviderEnabled(locationManager.GPS_PROVIDER)){
startUpdates();
} else {
// I open here the preferences to force the user start the GPS
}
}
private void startUpdates(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
public class LocListener implements LocationListener{
@Override
public void onLocationChanged(Location loc){
...
locationManager.removeUpdates(locationListener);
scheduleUpdates();
}
...
}
private void scheduleUpdates(){
// Wait 5 minutes
handler.sleep(5 * 60 * 1000);
startUpdates();
}
class WaitHandler extends Handler {
@Override
public void handleMessage(Message message) {}
public void sleep (long delayMillis){
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
}
I have been working on this for hours, but I haven't been able to find a good solution yet. Any help would be appreciated,
Thank you very much,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您应该使用 requestLocationUpdate 参数:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5*60*1000, 0, locationListener);
Maybe you should use the requestLocationUpdate argument:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5*60*1000, 0, locationListener);
定义 Timer t 和 Handler 处理程序,并这样写:
delay 是第一次运行之前的毫秒数,每次运行将由rep 毫秒分隔(在您的情况下,rep = 1000*60*5)。
Define the Timer t and the Handler handler, and write this:
delay is the number of milliseconds before the first run, and each run will be seperated by rep milliseconds (in your case, rep = 1000*60*5).