如何从AsyncTask调用“requestLocationUpdates”

时间:2012-10-03 09:50:55

标签: android

我有一个AsyncTask来检查Accelerometer数据。当我检测到Accelerometer提供更大的值时,我需要开始GPS记录。

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()

1 个答案:

答案 0 :(得分:1)

AsyncTask不会在Looper线程上运行,因此问题,您可能希望使用Handler,因为它们与Looper线程一起运行,因此不会发生此类错误。