gpt4 book ai didi

android - GPS Android 中的 LocationManager requestLocationUpdates()

转载 作者:行者123 更新时间:2023-11-30 03:32:41 24 4
gpt4 key购买 nike

我开发了一个应用程序作为处理基本 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)

如果我必须为此使用处理程序/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/主线程,您应该能够使用 runOnUiThread 创建一个新的可运行对象,如下所示。

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

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

关于android - GPS Android 中的 LocationManager requestLocationUpdates(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17171886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com