Мое приложение аварийно завершает работу при обращении к службе определения местоположения в методе, который вызывается в потоке. Я передаю контекст из службы, в которой запущен поток, но он не работает.
Это вызов LocationService
в потоке, который создается внутри службы.
class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread thread = new Thread(){
@Override
public void run() {
try{
geofix = new GeoFixer(getApplicationContext());
geofix.startLocationObserver();
...
}catch (....){}....
Конструктор GeoFixer
берет контекст и сохраняет его в Context context
.
Теперь внутри метода startLocationObserver()
я вызываю getSystemService
с этим контекстом, который завершает работу приложения.
public class GeoFixer {
...
private Context context;
GeoFixer(Context context){
this.context = context;
}
public void startLocationObserver(){
LocationManager locationManager = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
// This is where it crashes
... }
Что я делаю не так?
РЕДАКТИРОВАТЬ: Вот LogCat сейчас.
ERROR/AndroidRuntime(8824): FATAL EXCEPTION: Thread-10
ERROR/AndroidRuntime(8824): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
ERROR/AndroidRuntime(8824): at android.os.Handler.<init>(Handler.java:121)
ERROR/AndroidRuntime(8824): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:173)
ERROR/AndroidRuntime(8824): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:173)
ERROR/AndroidRuntime(8824): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:579)
ERROR/AndroidRuntime(8824): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
ERROR/AndroidRuntime(8824): at de.theapplication.GeoFixer.getNetworkLocation(GeoFixer.java:64)
ERROR/AndroidRuntime(8824): at de.theapplication.GeoFixer.startLocationObserver(GeoFixer.java:27)
ERROR/AndroidRuntime(8824): at de.theapplication.Fadenzieher$1.run(Fadenzieher.java:36)