2011-05-10 40 views
0

有沒有辦法從特定intervan的locationManager請求位置更新並忽略minDistance?我試過android locationManager requestLocationUpdates

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3600 * 1000, 1, this) 

但在日誌中有時會出現該位置是在幾分鐘內更新,有時小時的一半......可以這樣做在一個固定的時間間隔更新位置信息,而忽略距離是多少?

+0

你試過將minDistance(3rd參數)設置爲0嗎? – 2011-05-10 09:25:26

+0

是的,我已經嘗試過0和-1,但結果相同... – 2011-05-10 09:27:35

回答

1

您可以使用此架構。創建新的Runnable,每次都會調用,當你需要它時

private final Handler _handler = new Handler(); 
private static int DATA_INTERVAL = 60 * 1000; 

private final Runnable getData = new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     getDataFrame(); 
    } 
}; 
private void getDataFrame() 
{ 
    requestGPS(); 
    Timer time = new Timer(); 
    time.schedule(new TimerTask() { 
      @Override 
      public void run() { 
       _locationManager.removeUpdates(_connector); 
       this.cancel(); 
      } 
     }, 5000, 5000); 
    _handler.postDelayed(getData, DATA_INTERVAL); 
} 
private void requestGPS() 
{ 
    _locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 
       0, 
       0, 
       _connector); 
} 
+0

可以將該代碼用於遠程服務,我想獲取用戶位置? – noloman 2011-08-06 14:28:21

+2

不能,因爲我不斷地得到異常'不能被稱爲形式的服務'不能在線程中創建處理程序,而不是在Android中調用Looper準備。 – noloman 2011-08-06 14:54:36

相關問題