5

I have this onConnected call back implemented for Google LocationClient API:

@Override
public void onConnected(Bundle arg0) {
    if (lc != null) {
        lastKnownLocation = lc.getLastLocation();
        LocationRequest request = new LocationRequest();
        request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        request.setFastestInterval(MIN_TIME_BETWEEN_LOCATION_UPDATES);
        request.setSmallestDisplacement(MIN_DISTANCE_BETWEEN_LOCATION_UPDATES);
        lc.requestLocationUpdates(request, ll);
    }
}

And for some reason, sometimes this line:

lastKnownLocation = lc.getLastLocation();

Gives me this exception:

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
   at com.google.android.gms.internal.de.bc()
   at com.google.android.gms.internal.ez.a()
   at com.google.android.gms.internal.ez$c.bc()
   at com.google.android.gms.internal.ey.getLastLocation()
   at com.google.android.gms.internal.ez.getLastLocation()
   at com.google.android.gms.location.LocationClient.getLastLocation()
   at com.citylifeapps.cups.helputils.UserLocation.onConnected(UserLocation.java:115)
   at com.google.android.gms.internal.de.aZ()
   at com.google.android.gms.internal.de$f.a()
   at com.google.android.gms.internal.de$f.a()
   at com.google.android.gms.internal.de$b.be()
   at com.google.android.gms.internal.de$a.handleMessage()
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:157)
   at android.app.ActivityThread.main(ActivityThread.java:5356)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
   at dalvik.system.NativeStart.main(NativeStart.java)

I really can't understand why this happens. Clearly I'm trying to get the last known location using the LocationClient after running the connect method and waiting for the onConnected callback and as I understand the onConnected call back is called when I have connection, So how can I get the "Not connected ..." exception? Does some one knows?

Thanks.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187

4 Answers4

3

Maybe like me, you did not call connect() at all. Try with the call included in the onStart method:

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
sethi.anuj
  • 39
  • 5
0

i have the same issue on Samsung S4 and S3 mini.

Just wrap with try/catch getLastLocation() and start to getting actual location from requirstLocationUpdate:

@Override
public void onConnected(Bundle arg0) {
    if (lc != null) {
        try{   
           lastKnownLocation = lc.getLastLocation();
        catch(Exception ex){ /* log error */ }

        LocationRequest request = new LocationRequest();
        request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        request.setFastestInterval(MIN_TIME_BETWEEN_LOCATION_UPDATES);
        request.setSmallestDisplacement(MIN_DISTANCE_BETWEEN_LOCATION_UPDATES);
        lc.requestLocationUpdates(request, ll);
    }
}
Imaginary
  • 259
  • 2
  • 9
  • 1
    No it's not that simple, wrapping this line with try and catch may remove the crashes but a lot of my users complain on the location detection problems. So this does not solve my problem. – Emil Adz May 22 '14 at 12:04
  • usually it crashed on getting last location only but you can get actual location. – Imaginary Jun 11 '14 at 08:51
0

I tried this:

@Override
public void onConnected(Bundle arg0) {
    Log.w(TAG, "onConnected");
    if (!mLocationClient.isConnecting()) {
        mLocationClient.getLastLocation();
    }
}

So far it works fine for me but I have absolutely no idea why.

Tamaki Sakura
  • 482
  • 5
  • 22
0

A little late to the party, but just in case someone stumbles into this problem, too. In my case, I got the same error message as Emil. After some debugging I noticed that (in my specific code) I called mLocationClient.connect() twice, the second time with a little delay of approx. 100 milliseconds. I believe the first call caused the onConnect() callback to be triggered, but then the second connect() call broke the connection.

The solution in my case was to check before connecting, if there is not already a connection, similar as Extended Range suggests it above. I'm not sure why Google disconnects the mLocationClient on a repeated connect() call, but in my case it solved the problem.

if (!mLocationClient.isConnected() && !mLocationClient.isConnecting()){
    mLocationClient.connect();
}
peitek
  • 884
  • 2
  • 17
  • 27