GPS安卓中的LocationManager requestLocationUpdates()

时间:2013-06-18 14:44:11

标签: android gps looper

我开发了一个应用程序作为处理基本HTTP请求的服务。当手机收到HTTP Post请求时,如:http ://IP:port/gps/on,它应该注册到GPS监听器,如下所示:

 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,200,0,locationListener); 

但是,由于此代码存在于处理程序中,我收到以下错误:

8.614: E/AndroidRuntime(21211): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-18 12:34:58.614: E/AndroidRuntime(21211):    at android.os.Handler.<init>(Handler.java:121)
06-18 12:34:58.614: E/AndroidRuntime(21211):    at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:183)
06-18 12:34:58.614: E/AndroidRuntime(21211):    at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:183)
06-18 12:34:58.614: E/AndroidRuntime(21211):    at android.location.LocationManager._requestLocationUpdates(LocationManager.java:661)
06-18 12:34:58.614: E/AndroidRuntime(21211):    at android.location.LocationManager.requestLocationUpdates(LocationManager.java:486)
06-18 12:34:58.614: E/AndroidRuntime(21211):    at com.example.devicecommunication.ConnectService$HttpFileHandler.registerGPS(ConnectService.java:4281)
06-18 12:34:58.614: E/AndroidRuntime(21211):    at com.example.devicecommunication.ConnectService$HttpFileHandler.handle(ConnectService.java:700)
06-18 12:34:58.614: E/AndroidRuntime(21211):    at org.apache.http.protocol.HttpService.doService(HttpService.java:243)
06-18 12:34:58.614: E/AndroidRuntime(21211):    at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:187)
06-18 12:34:58.614: E/AndroidRuntime(21211):    at com.example.devicecommunication.ConnectService$WorkerThread.run(ConnectService.java:4987) 

如果我必须使用handler / Looper,请告诉我吗?以及如何做到这一点的任何例子。提前谢谢!

注册GPS的代码由处理HTTP请求的类调用:

public String registerGPS(){
String gps= "";
if(!gpsSensor){
if(isGpsOn(appContext))
{
Log.d("GPS on","Using GPS Provider here"); 
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,200,0,locationListener);
gps = "***********GPS Provider registered here *********************";
}
else
{
Log.d("GPS off","using NETWORK Provider here");
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,200,0,locationListener);
gps = "***********Network provider registered here *********************";
}
}
gpsSensor = true;
return gps; 
}

3 个答案:

答案 0 :(得分:1)

你得到的错误是因为你试图从需要主线程的后台线程运行某些东西。您提供的代码并未向我显示此线程的设置,但为了将位置设置回UI /主线程,您应该能够使用runOnUiThread创建一个新的runnable,如下所示。

private Runnable runnable = new Runnable() {
public void run() {  
    runOnUiThread(new Runnable() { 
        public void run(){ 
            //set up your listener here
        } 
    }); 
}

只是另一个评论,你提到GPS,UI /主要线程动作,HTTP发布它听起来像是可以使用AsyncTask设置。我必须看到更多的代码,但如果你没有对它进行调查,这可能会让你的生活变得更轻松。

答案 1 :(得分:0)

public class LocationLog extends Thread {
    LocationManager lm;
    LocationHelper loc;
    String latString, lngString = null;

    public LocationLog(Context context, String latString, String lngString) {
        loc = new LocationHelper();
        lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    }

    public void run() {
        Looper.prepare();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, loc);
        Looper.loop();
    }
}

然后在您的服务类中使用:

private void LocationLog() {
        new LocationLog(context, latString, lngString).start();
        Log.d(latString, getClass().getSimpleName());
        Log.d(lngString, getClass().getSimpleName());
        retrieveUserId();
        sendData(user_id);
    }

答案 2 :(得分:0)

在我的Handler for Post中,我将一个额外的字符串传递给我的服务并再次启动该服务。在onStartCommand()中,我从intent中获取额外内容,然后调用requestLocationUpdates(),因此它在主线程上完成。这解决了我的要求。谢谢大家的帮助!