2012-10-03 38 views
0

我有一個AsyncTask來檢查加速度計數據。當我發現加速度傳感器給出更大的值時,我需要啓動GPS記錄。如何從AsyncTask調用「requestLocationUpdates」

private LocationManager mlocManager; 
private GPSLocationListener mlocListener; 
.. 
mlocManager = (LocationManager)context.getSystemService(context.LOCATION_SERVICE); 
mlocListener = new GPSLocationListener(); 
.. 
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 100, mlocListener); 
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 100, mlocListener); 

如何使其運行?我得到的錯誤是

error occured while executing doInBackground() 
Can't create handler inside thread that has not called Looper.prepare() 
+0

爲什麼你需要使用AsyncTask來做到這一點?如果你真的想在你的AsyncTask中使用它,請在onPreExecute()中使用它,以便它在UI線程上啓動 –

+0

檢查此鏈接http://stackoverflow.com/questions/6961611/android-requestlocationupdates-throws-exception – Antrromet

回答

1

的AsyncTask不彎針線,因此這個問題上運行,你可能想,因爲他們與套口線運行,因此這樣的錯誤不會發生使用的處理程序。