GoogleApiClient is throwing “GoogleApiClient is not connected yet” AFTER onConnected function getting called

断了今生、忘了曾经 提交于 2019-11-26 20:18:40

I just noticed that you are creating the googleApiClient in onStartCommand(). This seems like a bad idea.

Let's say that your service gets triggered twice. Two googleApiClient objects will get created, but you'll only have reference to one. If the one whose reference you don't have executes its callback to onConnected(), you will be connected in that client but the client whose reference you actually do have could still be unconnected.

I suspect that's what's going on. Try moving your googleApiClient creation to onCreate and see if you get the same behavior.

I had the same error, but my problem was different from iheanyl's answer:

I had declared the googleApiClient static. This prevented android from shutting down the service.

I had this problem to and I solved it with declaring googleApiClient as static object

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.

The implementation of the GoogleApiClient appears designed for only a single instance. It's best to instantiate it only once in onCreate, then perform connections and disconnections using the single instance.

call connect() method in onStart()method

 @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();
       }

I think I found the root of the problem and it has nothing to do with the API. I have faced this issued every time I install the app. By the way just like the most popular comment I only have one googleApiClient upon pause and resume. What I noticed is that the permission request to get geolocation access pops up. As you know in your activity this is going to turn into a pause, and once the popup is gone it resumes your activity. Most likely your googleClient is also chained to those events to disconnect and connect. You can tell there is no connection once the permission is accepted and you are about to perform the googleApiClient task.

 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
                    }
                });
    }

You can skip disconnecting and connecting as you set a flag which is true before permission and once the googleApiClient task is completed or granted is false.

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;
                    }
                });
    }

We could also split doing permission alone, and if granted then make the task call. Ok, I am going to confess I am using a fragment, and I reinstalled the app even rotated the screen on permissions and once granted, the result showed up. I hope this helps other people.

  • You don't have to uninstall the app. Simply go to setting/application manager/your app/ permissions and turn any off, then test the app again.

I had this issue when I accidentaly but googleApiClient.connect() in oncreate and onresume. Just removed it from oncreate.

moving the googleApi creation to onCreate resolved the issue for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!