requestLocationUpdates无法从Thread工作

时间:2013-07-29 04:28:39

标签: android multithreading location

您好我正在尝试从UI线程以外的线程请求LocationUpdates,我在以下行中获得了RuntimeExeption:

// request location updates using Cellular 
lm.requestLocationUpdates(
    LocationManager.NETWORK_PROVIDER,
    0,
    0,
    locationListener);

然后阅读文档说它抛出:

IllegalArgumentException  if provider is null or doesn't exist on this device 
IllegalArgumentException  if listener is null 
RuntimeException  if the calling thread has no Looper 
SecurityException  if no suitable permission is present  

所以似乎我的线程没有Looper,但问题是我不知道他们的意思是“Looper”。提前谢谢!

2 个答案:

答案 0 :(得分:2)

将您的run()代码更改为

@Override
public void run()
{
    Looper.prepare();

    // The rest of your code

    Looper.loop();
}

答案 1 :(得分:0)

你可以试试这个:

lm.requestLocationUpdates(
    LocationManager.NETWORK_PROVIDER,
    0,
    0,
    locationListener,
    Looper.myLooper()
);