我想每隔10秒將一些數據和座標一起發送到遠程服務器。我想,這最匹配的將是locationListener只調用一次
public void onCreate(Bundle savedInstanceState) {
//snip
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, new SendingLocationListener());
}
在監聽我有以下代碼
:
public void onLocationChanged(Location location) {
if(null == location) return;
TrackerNotifierTask task = new TrackerNotifierTask();
task.execute(location);
}
的TrackerNotifierTask
使用httpclient
在它的doInBackground()
方法,所以這是非常簡單的。
現在,如果我開始活動,我可以看到onLocationChanged()
得到執行並且數據成功地到達遠程服務器。但只有一次!不管我以後做什麼,更換座標或任何東西,任務都不會被調用。
這是在android中執行此類事情的正確方法,還是我應該使用background-service
?
這是否解決問題了嗎? – Shrikant
好吧,與匿名內部類的技巧已經奏效,每個GPS(模擬)更新調用監聽器。另一方面,minTime似乎被忽略。有可能以某種方式強制它? – injecteer
請參閱http://stackoverflow.com/questions/6442093/how-do-i-decide-what-interval-to-use-for-requestlocationupdates,如果它解決了您的問題。 – Shrikant