新的Location API Android.错误"调用connect()并等待onConnected()被调用"

ang*_*0ne 1 android geolocation android-service android-location

我需要你宝贵的帮助;)

我有一个的Android服务在后台运行该固定位置的装置周期性地(一个位置轮询).当我使用使用Google Play服务的新Android Location API更新代码时.让我们来看看服务:

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();
        }
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

方法" areServicesConnected() "如下:

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;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

有时服务崩溃和日志如下:

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)
Run Code Online (Sandbox Code Playgroud)

你怎么看?我读过类似的帖子,但不是一回事.似乎有时候如果方法isServicesConnected()返回true,稍后由于某些原因将断开服务.

谢谢,每一个帮助都将非常感谢!

快乐的编码;)

And*_*son 6

isGooglePlayServicesAvailable 并不告诉您已连接,它只是检查服务的可用性.

你应该做你的logcat中的确切内容.LocationClient.connect()一旦你的代码到达ConnectionCallbacks.onConnected我在你的问题中没有看到的实现,你就会打电话给你.