从onConnected内部抛出的Android locationClient未连接异常

时间:2014-07-30 12:36:42

标签: android android-geofence location-client

我有一个Android应用程序,它使用地理围栏添加在以下代码中:

public void register(ArrayList<Geofence> geofences){
        if(geofences == null || geofences.size() == 0) return;
        this.geofences = geofences;
        locationClient = new LocationClient(context, this, onConnectionFailedListener);
        locationClient.connect();
    }

    @Override
    public void onConnected(Bundle bundle) {
        locationClient.addGeofences(geofences, GeofenceBroadcastReceiver.getPendingIntent(context), this);
    }

    @Override
    public void onDisconnected() {
        this.locationClient = null;
    }

    @Override
    public void onAddGeofencesResult(int statusCode, String[] strings) {
        if(statusCode != LocationStatusCodes.SUCCESS){
            Log.e(getClass().getName(), "onAddGeofencesResult: " + statusCode);
            return;
        }

        listener.addRegistered(strings);
        locationClient.disconnect();
    }

我偶尔会收到以下异常(Crashlytics链接:http://crashes.to/s/b63ac8d5f19):

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
   at com.google.android.gms.internal.ff.bT()
   at com.google.android.gms.internal.hc.addGeofences()
   at com.google.android.gms.location.LocationClient.addGeofences()
   at nl.auxilium.autoreset.GeofenceManager$AddGeofenceManager.onConnected(GeofenceManager.java:140)
   at com.google.android.gms.internal.ff$c.onConnected()
   at com.google.android.gms.internal.fg.b()
   at com.google.android.gms.internal.fg.bV()
   at com.google.android.gms.internal.ff$h.b()
   at com.google.android.gms.internal.ff$h.a()
   at com.google.android.gms.internal.ff$b.eN()
   at com.google.android.gms.internal.ff$a.handleMessage()
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:158)
   at android.app.ActivityThread.main(ActivityThread.java:5751)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
   at dalvik.system.NativeStart.main(NativeStart.java)

鉴于异常,locationClient未连接,但代码是onConnected方法的宝座。

任何人都可以指出我正确的方向,我似乎无法在我开发的Galaxy S4上重现这个错误。

2 个答案:

答案 0 :(得分:0)

您应该设置连接请求

locationClient.requestLocationUpdates(REQUEST, this);

在这里阅读更多内容 https://stackoverflow.com/a/22387816

答案 1 :(得分:0)

以下是我解决此问题的方法。我只是捕获IllegalStateException然后处理will事件,好像没有位置提供者存在(ShowDefaultLocationData()是在没有GPS的情况下处理事情的方法。)

private void requestLocation() {
        if (servicesConnected()) {
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                updateLocation = true;
                mLocationRequest = LocationRequest.create();
                mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                mLocationRequest.setInterval(5000);
                mLocationRequest.setFastestInterval(1000);
                mLocationClient = new LocationClient(this, this, this);
                mLocationClient.connect();
            }
        } else
        {
            ShowDefaultLocationData();
        }
    }


@Override
public void onConnected(Bundle connectionHint) {
    try {
        mLocationClient.requestLocationUpdates(mLocationRequest, MainActivity.this);
        isconnected = true;
    } catch (IllegalStateException e) {
        ShowDefaultLocationData();
    }
}