2016-10-26 106 views
0

Iam開發一個應用程序,通過使用Handler每秒執行一些場信號強度計算,並且在處理程序期間它記錄座標並記錄結果&座標。它工作正常,除非真正的測試和我增加車輛的記錄座標不是每秒鐘的速度,而有時每2-3-4秒不接受我。 下面的代碼:位置更新


final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,  0, this); 

publicvoid method_01(LocationManager locationManager){ 

final Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 

@Override 
public void run() { 
Location locationDataRate =  locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
double latitude = locationDataRate.getLatitude(); 
double longitude = locationDataRate.getLongitude(); 
x=y+1; 
Save to file (latitude,longitude,x); 
handler.postDelayed(this, 1000); 
} 
}, 1000); 

} 

我曾嘗試也改變了時間和分鐘。距離(1000,0)

+0

您需要訂閱位置更改 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,1f,yourListener); –

回答

0

首先,如果您希望每1秒接收一次更新,則不應請求更頻繁的更新。因此:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this); 

總之,處理位置更新的最佳做法是不是永久循環getLastKnownLocation但執行位置更新的回調。有一堆示例here

你的問題,在doc他們的狀態更新之間的間隔從未保證:

的位置更新間隔可以使用minTime 參數來控制。位置更新之間所用的時間永遠不會比minTime小 ,儘管它可能更多取決於位置 提供程序實現和其他 應用程序請求的更新時間間隔。