开发者

Setting an interval between every GPS fix

开发者 https://www.devze.com 2023-02-28 20:15 出处:网络
I am trying to set up a 5 minutes interval between every GPS fix in Android. My code looks l开发者_如何学Pythonike this:

I am trying to set up a 5 minutes interval between every GPS fix in Android. My code looks l开发者_如何学Pythonike 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,


Maybe you should use the requestLocationUpdate argument: locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5*60*1000, 0, locationListener);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消