在连接函数调用后,GoogleApiClient正在抛出“GoogleApiClient尚未连接”

时间:2021-12-15 18:57:50

So i found something that is not very clear for me about GoogleApiClient. GoogleApiClient has a function called onConnected which is ran when the client is connected (for sure)

所以我发现了一些关于GoogleApiClient的东西并不是很清楚。 GoogleApiClient有一个名为onConnected的函数,它在客户端连接时运行(肯定)

I got my own function called: startLocationListening which is eventually getting called on GoogleApiClient's onConnected function.

我得到了自己的函数:startLocationListening,最终在GoogleApiClient的onConnected函数上调用。

So my startLocationListening function couldnt ran without a GoogleApiClient connection.

所以我的startLocationListening函数无法在没有GoogleApiClient连接的情况下运行。

Code and log:

代码和日志:

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

The exception is:

例外是:

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)

...

...

My debug log also says the onConnected function got called:

我的调试日志还说调用了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

After this i got the exception.

在此之后我得到了例外。

Am I missing something here? I got a response for "connected" i ran my func, and i got the error "not connected" whats this? Plus one annoying thing is: I used this location service for weeks now and never got this error.

我在这里错过了什么吗?我得到了“连接”的响应我运行了我的功能,我得到错误“没有连接”这是什么?另外一个令人讨厌的事情是:我现在使用这个位置服务已经好几周了,从来没有出现过这个错误

E D I T :

E D I T:

I added a more specific log output, just blown my mind, check this out:

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

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

log output in this case:

这种情况下的日志输出:

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.

Yes, i just got mGoogleApiClient.isConnected() == false inside onConnected() how is it possible?

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

E D I T:

E D I T:

Since nobody could answer this even with reputation bounty, i decided to report this as a bug to Google. What next came was really surprised me. Google's official answer for my report:

由于没有人能够以声誉赏金回答这个问题,我决定将此报告为谷歌的一个错误。接下来的事真的让我感到惊讶。 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. "

“这个网站是针对AOSP Android源代码和开发人员工具集的开发人员问题,而不是Google应用程序或服务,如Play服务,GMS或Google API。不幸的是,似乎没有合适的地方来报告Play服务的错误我只能说这个网站不是,对不起。请尝试在Google产品论坛上发帖。“

Full issue report here. (I hope they wont remove it just because its silly)

完整问题报告在这里。 (我希望他们不会因为愚蠢而删除它)

So yeah i took a look at Google Product Forums and just coudnt find any topic to post this thing, so at the moment Iam puzzled and stuck.

所以是的,我看了一下谷歌产品论坛,只是找到任何话题来发布这个东西,所以此刻我感到困惑和困惑。

Does anyobody in planet earth could help me with this?

地球上的任何人都可以帮助我吗?

E D I T:

E D I T:

Full code in pastebin

pastebin中的完整代码

8 个解决方案

#1


77  

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

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

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.

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

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

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

#2


2  

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

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

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

我已经宣布googleApiClient是静态的。这阻止了android关闭服务。

#3


0  

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

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.

您应该在Activity的onCreate(Bundle)方法中实例化一个客户端对象,然后在onStart()中调用connect(),在onStop()中调用disconnect(),而不管状态如何。

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.

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

#4


0  

call connect() method in onStart()method

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

#5


0  

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.

我想我找到了问题的根源,它与API无关。我每次安装应用程序时都会遇到此问题。顺便说一下,就像最受欢迎的评论一样,我暂停和恢复时只有一个googleApiClient。我注意到,弹出了获取地理位置访问权限的权限请求。正如您在活动中所知,这将暂停,一旦弹出窗口消失,它将恢复您的活动。很可能你的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
                    }
                });
    }

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.

您可以跳过断开连接和连接,因为您在许可之前设置了一个标志,并且完成或授予googleApiClient任务后为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.
  • 您不必卸载该应用程序。只需转到设置/应用程序管理器/您的应用程序/权限,然后关闭,然后再次测试应用程序。

#6


0  

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

我有这个问题,我通过将googleApiClient声明为静态对象来解决它

#7


-1  

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

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

#8


-4  

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

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

#1


77  

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

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

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.

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

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

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

#2


2  

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

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

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

我已经宣布googleApiClient是静态的。这阻止了android关闭服务。

#3


0  

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

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.

您应该在Activity的onCreate(Bundle)方法中实例化一个客户端对象,然后在onStart()中调用connect(),在onStop()中调用disconnect(),而不管状态如何。

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.

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

#4


0  

call connect() method in onStart()method

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

#5


0  

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.

我想我找到了问题的根源,它与API无关。我每次安装应用程序时都会遇到此问题。顺便说一下,就像最受欢迎的评论一样,我暂停和恢复时只有一个googleApiClient。我注意到,弹出了获取地理位置访问权限的权限请求。正如您在活动中所知,这将暂停,一旦弹出窗口消失,它将恢复您的活动。很可能你的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
                    }
                });
    }

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.

您可以跳过断开连接和连接,因为您在许可之前设置了一个标志,并且完成或授予googleApiClient任务后为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.
  • 您不必卸载该应用程序。只需转到设置/应用程序管理器/您的应用程序/权限,然后关闭,然后再次测试应用程序。

#6


0  

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

我有这个问题,我通过将googleApiClient声明为静态对象来解决它

#7


-1  

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

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

#8


-4  

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

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