1

I need your precious help ;)

I have an Android service that runs in background that fix the location of the device periodically (a location poller). When I have updated the code with the new Android Location API that makes use of the Google Play Services. Lets take a look to the service:

public class NewLocationPollerService extends Service implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener
{
    private LocationRequest mLocationRequest    = null;
    private LocationClient  mLocationClient     = null;

    ...
    private class PollerThread extends WakefulThread
    {
        protected void onPreExecute()
        {
            if ( GooglePlayServicesUtility.areServicesConnected( NewLocationPollerService.this ) )
                mLocationClient.requestLocationUpdates(mLocationRequest, intent);
        }

        protected void onPostExecute()
        {
            if ( GooglePlayServicesUtility.areServicesConnected( NewLocationPollerService.this ) )
                mLocationClient.removeLocationUpdates(intent);

            super.onPostExecute();
        }
    }
    ...
}

The method "areServicesConnected()" is the following:

public class GooglePlayServicesUtility
{
    private static final String TAG = GooglePlayServicesUtility.class.getSimpleName();

    public static boolean areServicesConnected(Context context)
    {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
        if (ConnectionResult.SUCCESS == resultCode)
            return true;
        else
            return false;
    }
    ...
}

Sometimes the service crash and the log is the following:

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.gms.internal.k.B(Unknown Source)
at com.google.android.gms.internal.bh.a(Unknown Source)
at com.google.android.gms.internal.bh$c.B(Unknown Source)
at com.google.android.gms.internal.bg.requestLocationUpdates(Unknown Source)
at com.google.android.gms.internal.bh.requestLocationUpdates(Unknown Source)
at com.google.android.gms.location.LocationClient.requestLocationUpdates(Unknown Source)
at     me.app.location.NewLocationPollerService$PollerThread.onPreExecute(NewLocationPollerService.java:210    )
at me.app.thread.WakefulThread.onLooperPrepared(WakefulThread.java:79)
at android.os.HandlerThread.run(HandlerThread.java:59)
at me.app.thread.WakefulThread.run(WakefulThread.java:94)

What do you think? I read a similar post, but is not the same thing. It seems that sometimes also if the method areServicesConnected() returns true, later the services will be disconnected for some reasons.

Thanks, every help will be really appreciated!

Happy coding ;)

Community
  • 1
  • 1
angelst00ne
  • 39
  • 2
  • 7

1 Answers1

5

isGooglePlayServicesAvailable does not tell you that you are connected, it just checks the availability of the service.

You should do exactly what's in your logcat. Call LocationClient.connect() and you'll be connected once your code reaches your implementation of ConnectionCallbacks.onConnected which I don't see in your question.

Androiderson
  • 16,865
  • 6
  • 62
  • 72