Android 设置每个GPS定位之间的间隔
我正在尝试在Android中设置每个GPS定位之间的5分钟间隔。我的代码如下所示:Android 设置每个GPS定位之间的间隔,android,timer,location,Android,Timer,Location,我正在尝试在Android中设置每个GPS定位之间的5分钟间隔。我的代码如下所示: private void startMonitoring() { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationListener = new LocListener(); if (locationManager.isProviderEnabled(locatio
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);
}
}
我已经为此工作了几个小时,但还没有找到一个好的解决方案。任何帮助都将不胜感激
非常感谢,定义计时器t和处理程序,并编写以下内容:
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)。也许您应该使用requestLocationUpdate参数:
locationManager.RequestLocationUpdate(locationManager.GPS_提供程序,5*60*1000,0,locationListener)代码>我无法使用此功能,因为我需要暂停请求GPS更新以节省电池,这将使GPS保持打开状态。谢谢。另外,requestLocationUpdate参数只定义了修复之间的最小间隔,而不是实际间隔。