基本的な 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)
これにハンドラ/ルーパーを使用する必要があるかどうか教えてください。そして、これを行う方法の例。前もって感謝します!
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;
}