【问题标题】:How to call "requestLocationUpdates" from a AsyncTask如何从 AsyncTask 调用“requestLocationUpdates”
【发布时间】:2012-10-03 17:56:21
【问题描述】:

我有一个 AsyncTask 用于检查加速度计数据。当我检测到加速度计给出更大的值时,我需要开始 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()

【问题讨论】:

  • 为什么需要使用 AsyncTask 来做这个?如果你真的想在 AsyncTask 中使用它,请在 onPreExecute() 中使用它,这样它就会在 UI 线程上启动

标签: android


【解决方案1】:

AsyncTask 不在 Looper 线程上运行,因此存在问题,您可能希望使用处理程序,因为它们在 Looper 线程上运行,因此不会发生此类错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多