【问题标题】:LocationManager requestLocationUpdates() in GPS AndroidGPS Android 中的 LocationManager requestLocationUpdates()
【发布时间】:2013-06-14 20:17:06
【问题描述】:

我开发了一个应用程序作为处理基本 HTTP 请求的服务。当手机收到类似http ://IP:port/gps/on 的 HTTP Post 请求时,它应该注册到 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) 

如果我必须为此使用处理程序/ 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; 
}

【问题讨论】:

  • 你在哪里打这个电话?这通常只是意味着您需要使用 UI 线程而不是在后台执行操作。
  • 需要查看更多代码,您将 requestLocationUpdates 放在哪里,我猜是在处理程序或某种类型中。显示更多代码。
  • @Rarw 您好,感谢您的回复。我有一个线程创建一个处理所有请求的类。在 POST 处理程序中,我需要向侦听器注册 GPS。这是我的要求。

标签: android gps looper


【解决方案1】:

您遇到的错误是因为您试图从需要主线程的后台线程运行某些东西。您提供的代码没有显示该线程的设置,但要将位置设置回 UI/主线程,您应该能够使用 runOnUiThread 创建一个新的可运行对象,如下所示。

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

只是另一条评论,您提到了 GPS、UI/主线程操作和 HTTP 发布,听起来这可以使用 AsyncTask 进行设置。我必须查看更多代码,但如果你还没有仔细研究它,那可能会让你的生活更轻松。

【讨论】:

  • 我想我不能使用服务中的 runOnUiThread()。请告诉我您的建议
  • 我不认为这种说法是 100% 正确的。 runOnUiThread 是一个 Activity 方法,所以我敢打赌,只要您将适当的上下文传递给您的服务,您就可以访问它。但是,如果我错了,here's a post on using a runnable in a service vs an activity 以及如何从两者访问 UI 线程。
【解决方案2】:
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);
    }

【讨论】:

  • 谢谢.. 这是否意味着您要将它带到 UI 线程?我应该在哪里退出 Looper?
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 2014-04-03
    • 2012-06-10
    • 1970-01-01
    相关资源
    最近更新 更多