GoogleApiClient 在调用 onConnected 函数后抛出 "GoogleApiClient is not connected yet"

GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected function getting called

所以我发现了一些关于 GoogleApiClient 对我来说不是很清楚的东西。 GoogleApiClient 有一个名为 onConnected 的函数,当客户端 connected(当然)。

我有自己的函数调用:startLocationListening 最终 在 GoogleApiClient 的 onConnected 函数上被调用。

所以如果没有 GoogleApiClient 连接,我的 startLocationListening 函数无法 运行。

代码和日志:

@Override
public void onConnected(Bundle bundle) {
    log("Google_Api_Client:connected.");
    initLocationRequest();
    startLocationListening(); //Exception caught inside this function
}

...

private void startLocationListening() {
    log("Starting_location_listening:now");

    //Exception caught here below:
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
   }

例外情况是:

03-30 12:23:28.947: E/AndroidRuntime(4936):     java.lang.IllegalStateException: GoogleApiClient is not connected yet.
03-30 12:23:28.947: E/AndroidRuntime(4936):     at com.google.android.gms.internal.jx.a(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936):     at com.google.android.gms.common.api.c.b(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936):     at com.google.android.gms.internal.nf.requestLocationUpdates(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936):     at hu.company.testproject.service.GpsService.startLocationListening(GpsService.java:169)
03-30 12:23:28.947: E/AndroidRuntime(4936):     at hu.company.testproject.service.GpsService.onConnected(GpsService.java:259)

...

我的调试日志还说 onConnected 函数被调用:

03-30 12:23:28.847: I/Locationing_GpsService(4936): Google_Api_Client:connected.
03-30 12:23:28.857: I/Locationing_GpsService(4936): initLocationRequest:initing_now
03-30 12:23:28.877: I/Locationing_GpsService(4936): initLocationRequest:interval_5000
03-30 12:23:28.897: I/Locationing_GpsService(4936): initLocationRequest:priority_100
03-30 12:23:28.917: I/Locationing_GpsService(4936): Starting_location_listening:now

之后我得到了异常。

我是不是漏掉了什么?我收到 "connected" I 运行 my func 的响应,但收到错误消息 "not connected" 这是什么?另外一件烦人的事情是:我使用这个定位服务已经好几个星期了,从来没有出现过这个错误。

编辑:

我添加了一个更具体的日志输出,让我大吃一惊,看看这个:

@Override
    public void onConnected(Bundle bundle) {

        if(mGoogleApiClient.isConnected()){
            log("Google_Api_Client: It was connected on (onConnected) function, working as it should.");
        }
        else{
            log("Google_Api_Client: It was NOT connected on (onConnected) function, It is definetly bugged.");
        }

        initLocationRequest();
        startLocationListening();
    }

这种情况下的日志输出:

03-30 16:20:00.950: I/Locationing_GpsService(16608): Google_Api_Client:connected.
03-30 16:20:00.960: I/Locationing_GpsService(16608): Google_Api_Client: It was NOT connected on (onConnected) function, It is definetly bugged.

是的,我刚刚在onConnected()里面得到了mGoogleApiClient.isConnected() == false,这怎么可能?

编辑:

由于即使有声誉赏金也没有人能回答这个问题,我决定将此作为错误报告给 Google。 接下来发生的事情让我感到非常惊讶。 Google对我的报告的官方回答:

"This website is for developer issues with the AOSP Android source code and the developer toolset, not Google apps or services such as Play services, GMS or Google APIs. Unfortunately there doesn't seem to be an appropriate place to report bugs with Play Services. All I can say is that this website isn't it, sorry. Try posting on the Google Product Forums instead. "

完整问题report here.(我希望他们不会因为它太傻就将其删除)

是的,我查看了 Google 产品论坛,但找不到关于 post 这件事的任何主题,所以此刻我感到困惑和困惑。

地球上有人可以帮我解决这个问题吗?

编辑:

完整代码在pastebin

我刚刚注意到您正在 onStartCommand() 中创建 googleApiClient。这似乎是个坏主意。

假设您的服务被触发了两次。将创建两个 googleApiClient 对象,但您只能引用其中一个。如果您没有其引用的那个执行其对 onConnected() 的回调,您将在该客户端中连接,但您实际拥有其引用的客户端可能仍未连接。

我怀疑这是怎么回事。尝试将您的 googleApiClient 创建移动到 onCreate 并查看您是否得到相同的行为。

我有同样的错误,但我的问题与iheanyl的回答不同:

我已将 googleApiClient 声明为静态。这阻止了 android 关闭服务。

将 googleApi 创建移动到 onCreate 为我解决了这个问题。

我偶然遇到了这个问题,但是 googleApiClient.connect() 在 oncreate 和 onresume 中。刚刚从 oncreate 中删除它。

https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.html

You should instantiate a client object in your Activity's onCreate(Bundle) method and then call connect() in onStart() and disconnect() in onStop(), regardless of the state.

GoogleApiClient 的实现似乎仅为单个实例设计。最好在 onCreate 中只实例化一次,然后使用单个实例执行连接和断开连接。

在onStart()方法中调用connect()方法

 @Override
       protected void onStart() {
           super.onStart();

           // Call GoogleApiClient connection when starting the Activity
           googleApiClient.connect();
       }

       @Override
       protected void onStop() {
           super.onStop();

           // Disconnect GoogleApiClient when stopping Activity
           googleApiClient.disconnect();
       }

我想我找到了问题的根源,它与API无关。每次安装应用程序时,我都会遇到这个问题。顺便说一下,就像最流行的评论一样,我在暂停和恢复时只有一个 googleApiClient。我注意到弹出获取地理定位访问权限的请求。正如您在 activity 中所知道的那样,这将变成暂停,一旦弹出窗口消失,它将恢复您的 activity。您的 googleClient 很可能也链接到这些事件以断开连接和连接。一旦权限被接受并且您将要执行 googleApiClient 任务,您就可以知道没有连接。

 if(googleApiClient.isConnected()){

        rxPermissions
                .request(Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(granted -> {

                 if (granted) {
                        //here it could be client is already disconnected!!     
                        _geolocateAddress(response);
                    } else {
                        // Oups permission denied
                    }
                });
    }

您可以跳过断开连接和连接,因为您设置了一个标志,该标志在获得许可之前为真,一旦 googleApiClient 任务完成或 granted 为假。

if(googleApiClient.isConnected()){
        isRequestingPermission = true;
        rxPermissions
                .request(Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(granted -> {
                    if (granted && googleApiClient.isConnected()) {
                        //Once the task succeeds or fails set flag to false
                        //at _geolocateAddress
                        _geolocateAddress(response);
                    } else {
                        // Oups permission denied
                        isRequestingPermission = false;
                    }
                });
    }

我们也可以单独拆分执行权限,如果授予则进行任务调用。好吧,我要承认我正在使用一个片段,我重新安装了应用程序,甚至根据权限旋转了屏幕,一旦获得许可,结果就会显示出来。我希望这对其他人有帮助。

  • 您不必卸载该应用程序。只需转到 setting/application manager/your app/ permissions 并关闭任何权限,然后再次测试该应用程序。

我遇到了这个问题,我通过将 googleApiClient 声明为静态对象解决了这个问题

将 GoogleApiClient 从 onStartCommand 移动到 onCeate 解决了我的问题