新的位置 API Android。错误“调用 connect() 并等待调用 onConnected()”

Posted

技术标签:

【中文标题】新的位置 API Android。错误“调用 connect() 并等待调用 onConnected()”【英文标题】:New Location API Android. Error "Call connect() and wait for onConnected() to be called" 【发布时间】:2013-10-08 03:43:05 【问题描述】:

我需要你的宝贵帮助;)

我有一个 Android 服务,它在后台运行定期修复设备的位置位置轮询器强>)。当我使用使用 Google Play 服务的new 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();
        
    
    ...

方法“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;
    
    ...

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

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)

你怎么看?我读了similar post,但不是一回事。似乎有时如果areServicesConnected()方法返回true,稍后服务将由于某些原因断开连接。

谢谢,每一个帮助将不胜感激!

编码愉快 ;)

【问题讨论】:

【参考方案1】:

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

您应该完全按照您的 logcat 中的内容进行操作。致电LocationClient.connect(),一旦您的代码到达ConnectionCallbacks.onConnected 的实现,您将被连接,我在您的问题中没有看到。

【讨论】:

您有关于如何实现 onconnected 的示例吗?检查 youtube 服务是否已连接

以上是关于新的位置 API Android。错误“调用 connect() 并等待调用 onConnected()”的主要内容,如果未能解决你的问题,请参考以下文章