在外部服务

时间:2015-05-13 12:02:53

标签: android service locationmanager looper

当我尝试在外部服务中运行的自定义类中使用LocationManager时,我收到以下异常:

*** 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()
        at android.os.Handler.<init>(Handler.java:200)
        at android.os.Handler.<init>(Handler.java:114)
        at android.location.LocationManager$GpsStatusListenerTransport$1.<init>(LocationManager.java:1464)
        at android.location.LocationManager$GpsStatusListenerTransport.<init>(LocationManager.java:1464)
        at android.location.LocationManager.addGpsStatusListener(LocationManager.java:1503)
        at org.poseidon_project.contexts.hardware.GPSIndoorOutdoorContext.start(GPSIndoorOutdoorContext.java:97)
        at org.poseidon_project.context.management.ContextManager.addObserverRequirement(ContextManager.java:97)
        at org.poseidon_project.context.reasoner.ContextMapper.registerIndoorOutdoorsContext(ContextMapper.java:260)
        at org.poseidon_project.context.reasoner.ContextMapper.registerContext(ContextMapper.java:197)
        at org.poseidon_project.context.ContextReasonerCore.addContextRequirement(ContextReasonerCore.java:70)
        at org.poseidon_project.context.ContextReasonerService$1.addContextRequirement(ContextReasonerService.java:74)
        at org.poseidon_project.context.IContextReasoner$Stub.onTransact(IContextReasoner.java:74)
        at android.os.Binder.execTransact(Binder.java:446)

现在,我已经阅读了许多关于使用Looper的答案,其中包括:

    Looper.prepare;
    mLocationManager.requestLocationUpdates(mProvider, mMinTime, mMinDistance, this, Looper.getMainLooper);

但最终没有导致在有更新时调用Callback(onLocationChanged(Location location))?

该类实现了LocationListener,它还调用了LocatioManager方法:

public abstract class LocationContext extends ContextObserver implements LocationListener {

protected LocationManager mLocationManager;
private int mMinTime = 3000;
private int mMinDistance = 10;
private String mProvider = LocationManager.GPS_PROVIDER;
private String mIdealProvider = LocationManager.GPS_PROVIDER;


public LocationContext (Context c) {
    super(c);
    mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}

public LocationContext (Context c, ContextReceiver cr) {
    super(c, cr);
    mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}

public LocationContext (Context c, ContextReceiver cr, int minTime, int minDistance, String name) {
    super(c, cr, name);
    mMinTime = minTime;
    mMinDistance = minDistance;
    mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}

public LocationContext (Context c, ContextReceiver cr, int minTime, int minDistance, String provider, String name) {
    super(c, cr, name);
    mMinTime = minTime;
    mMinDistance = minDistance;
    mProvider = provider;
    mIdealProvider = provider;
    mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}

@Override
public boolean start() {

    //new Thread(new Runnable() {
    //  @Override
    //  public void run() {
            //Looper.prepare();

            mLocationManager.requestLocationUpdates(mProvider, mMinTime, mMinDistance, this);
            //handler.sendEmptyMessage(0);
            //Looper.loop();
    //  }
    //}).start();


    mIsRunning = true;
    //Looper.loop();
    return true;
}


@Override
public boolean pause() {
    return stop();
}


@Override
public boolean resume() {
    return start();
}

@Override
public boolean stop() {
    mLocationManager.removeUpdates(this);
    mIsRunning = false;
    return true;
}

@Override
public void onLocationChanged(Location location) {
    checkContext(location);

}


protected abstract void checkContext(Location location);

@Override
public void onProviderDisabled(String provider) {
    if (provider.equals(mIdealProvider)) {
        mProvider = LocationManager.GPS_PROVIDER;

        if (! mLocationManager.isProviderEnabled(mProvider)) {
            Intent gpsOptionIntent = new Intent (android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(gpsOptionIntent);
        }
    }

}


@Override
public void onProviderEnabled(String provider) {
    if ((provider.equals(mIdealProvider)) && (! provider.equals(mProvider))) {
        mLocationManager.removeUpdates(this);
        mProvider = provider;
        mLocationManager.requestLocationUpdates(mProvider, mMinTime, mMinDistance, this);
    }
}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

public int getMinTime() {
    return mMinTime;
}

public void setMinTime(int mMinTime) {
    this.mMinTime = mMinTime;
}

public int getMinDistance() {
    return mMinDistance;
}

public void setMinDistance(int mMinDistance) {
    this.mMinDistance = mMinDistance;
}

我真的不明白如何在我的情况下使用Looper。有人可以帮忙吗?我理解答案“在UI线程中运行”,但这是一个单一的服务应用程序,没有UI,所以我认为我不能在UI线程中做到这一点?

****更新*****找到解决方案******

我相信我找到了解决方案。有问题的类是一个抽象类,我通过几个类进行了扩展,这些类执行了各种基于位置的操作。

在我使用的LocationContext抽象类中:

 mLocationManager.requestLocationUpdates(mProvider, mMinTime, mMinDistance,this, Looper.getMainLooper());

在一个实现类中(例如用于分析GPS卫星状态的类)我将它放在一个新的线程中:

new Thread(new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            GPSIndoorOutdoorContext.super.start();
            mLocationManager.addGpsStatusListener(gpsListener);
            Looper.loop();
        }
    }).start();

1 个答案:

答案 0 :(得分:0)

mLocationManager.requestLocationUpdates(mProvider, mMinTime, mMinDistance, this);

从非UI线程调用。确保在UI线程中调用init或调用方法。您可能无法从非UI线程发起LocationContext或调用start方法,但您无法做到这一点。要请求位置更新,必须从UI线程调用它。