设置每次 GPS 定位之间的间隔

发布于 2024-11-02 21:33:17 字数 1231 浏览 1 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞎闹 2024-11-09 21:33:17

也许您应该使用 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);

缱倦旧时光 2024-11-09 21:33:17

定义 Timer t 和 Handler 处理程序,并这样写:

t = new Timer();
        t.schedule(new TimerTask(){public void run(){handler.post(new Runnable(){public void run(){
        //your code here
}});}}, int delay, int rep );

delay 是第一次运行之前的毫秒数,每次运行将由rep 毫秒分隔(在您的情况下,rep = 1000*60*5)。

Define the Timer t and the Handler handler, and write this:

t = new Timer();
        t.schedule(new TimerTask(){public void run(){handler.post(new Runnable(){public void run(){
        //your code here
}});}}, int delay, int rep );

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文