Android locationClient not connected exception thrown from inside OnConnected

672 Views Asked by At

I have a Android application that uses geo-fences are being added in the following code:

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();
    }

Every once in a while i receive the following exception (Crashlytics link: 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)

Given the Exception the locationClient is not connected, but the code is being throne from the onConnected method.

Can anyone point me in the right direction, i can't seem to reproduce this bug on my develop Galaxy S4.

2

There are 2 best solutions below

0
On

You should set connect request

locationClient.requestLocationUpdates(REQUEST, this);

read more here https://stackoverflow.com/a/22387816

0
On

Below is how I fixed this issue. I simply catch the IllegalStateException and then deal will events as if no location provider is present (ShowDefaultLocationData() is the method which takes care of things if no GPS is available).

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();
    }
}