別プロセスで動作させているServiceでGPSを使う方法

android:process=":remote"なServiceではGPS(LocationManager)を利用できない - DenkiYagiの解決編。

現象

別プロセスで動作させているService(AndroidManifestでandroid:process=":remote"のように指定しているService)で、GPSを使おうとすると(LocationManager.requestLocationUpdates()をコールすると)エラーが発生する。LogCatには以下のようなエラーが表示される。

*** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

解決方法

別プロセスで動作させるときは明示的にLooperを指定してあげないといけないらしい。

Looper looper = Looper.getMainLooper();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, listener, looper);