GoogleApiClient throws "GoogleApiClient not yet connected" AFTER ONConnected call function

So, I found something that I don’t really understand about GoogleApiClient. GoogleApiClient has an onConnected function that runs when the client is connected (for sure)

I got my own function: startLocationListening , which ultimately gets a call to the GoogleApiClient onConnected function .

So my startLocationListening function could not work without a GoogleApiClient connection.

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.cb(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 that the onConnected function received a call :

 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 that I got an exception.

Am I missing something? I got a response for “connected,” I ran my func, and I got a “not connected” error, what is it? Plus one annoying thing: I used this location service for several weeks and never got this error.

ED i T:

I added a more specific log output, just blew 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 this possible?

ED i T:

Since no one could answer this even with gratitude for the reputation , I decided to report it as a bug for Google. What happened next really surprised me. The official Google response to my report:

“This website is intended for developers using the AOSP source for Android code and a set of developer tools, and not Google applications or services such as Play Services, GMS or Google APIs. Unfortunately, this seems to be a good place to report errors in Play Services. All I can say is that this site is wrong, sorry. Try posting on the Google Product Forum. "

Full release report here. (I hope they don’t remove it just because its stupid)

So, I looked at the Google Product Forums and just found some topic to post this stuff, so at the moment the pit is puzzled and stuck.

Can anyone on planet earth help me with this?

ED i T:

Full code in pastebin

+62
android google-play-services google-api-client
Mar 30 '15 at 10:32
source share
8 answers

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

Let's say that your service starts twice. Two googleApiClient objects will be created, but you will only have a link to one. If the one whose link you don’t have is making an onConnected () callback, you will be connected to that client, but the client whose link you really make can still be disconnected.

I suspect what is happening. Try moving the googleApiClient creation to onCreate and see if you get the same behavior.

+68
Apr 03 '15 at 22:55
source share

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

I declared googleApiClient statics. This prevented the android from disabling the service.

+2
Sep 28 '15 at 20:30
source share

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

You must create an instance of the client object in the Activity onCreate (Bundle) method, and then call connect () in onStart () and disconnect () in onStop () regardless of state.

A GoogleApiClient implementation is created for only one instance. It is best to create an instance only once in onCreate, and then perform connections and disconnections using a single instance.

0
Jul 19 '17 at 4:35 on
source share

call the connect () method in the 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(); } 
0
Aug 22 '17 at 14:25
source share

I think I found the root of the problem and has nothing to do with the API. I came across this every time I install the application. By the way, like the most popular comment, I have only one googleApiClient after a pause and resume. I noticed that a request appears to get access to geolocation. As you know, in your activity this will turn into a pause, and as soon as the pop-up window disappears, it will resume your activity. Most likely your googleClient is also tied to these events to disconnect and connect. You can say that after obtaining permission, there is no connection, and you are about to complete 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 when you set a flag that is true before permission and after the completion of the googleApiClient or granted task 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 share the permission to use, and if it was granted, then call the task call. Ok, I'm going to admit that I am using a snippet, and I reinstalled the application, even by rotating the screen at permissions and once providing the result. Hope this helps other people.

  • You do not need to uninstall the application. Just go to settings / application manager / your applications / permissions and disable them, and then check the application again.
0
01 Oct '17 at 22:02
source share

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

0
Dec 20 '17 at 16:34 on
source share

I had this problem when I accidentally, but googleApiClient.connect () in oncreate and onresume. Just deleted it from oncreate.

-one
May 31 '16 at 7:03 a.m.
source share

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

-four
May 16 '16 at 19:36
source share



All Articles