21

I am using GoogleApiClient in a service to request fused location updates. Every thing is working correctly, but sometimes the connection is suspended and onConnectionSuspended is called.

@Override
public void onCreate() {
    ...
    mGoogleApiClient = new GoogleApiClient.Builder(this) // this is a Context
    .addApi(LocationServices.API)
    .addConnectionCallbacks(this)  // this is a [GoogleApiClient.ConnectionCallbacks][1]
    .addOnConnectionFailedListener(this) //
    .build();

    mGoogleApiClient.connect();

    ...
}

@Override
public void onConnectionSuspended(int arg0) {

    // what should i do here ? should i call mGoogleApiClient.connect() again ? ? 

}

In the link above (ConnectionCallback doc) it says :

Applications should disable UI components that require the service, and wait for a call to onConnected(Bundle) to re-enable them.

But how this call to onConnected will happen ? should i call mGoogleApiClient.connect() again ? or the mGoogleApiClient will continue trying to connect even after a connection suspension ?

Tourki
  • 1,785
  • 15
  • 22

2 Answers2

36

GoogleApiClient will automatically try to reconnect. You do not need to call connect() again.

Hounshell
  • 5,321
  • 4
  • 34
  • 51
  • Hi Hounshell, thank you for your answer, but how can you be sure about it ? is there any doc saying so ? – Tourki Oct 02 '14 at 13:34
  • 23
    I'm the original author of GoogleApiClient. I'll try to add a documentation note for the next release that clarifies this. – Hounshell Oct 02 '14 at 19:44
  • @Hounshell Thank you. And the process of signing out from Google plus is tediously troublesome. Can you document that too please? There are dozens of questions regarding that on stackOverflow. The documentation is very narrow and misleading (as compared to that of Facebook). – VipulKumar Jan 12 '15 at 06:07
  • Sorry that you've had a rough experience with that. I've actually rolled off the project since then. I know that there was a team working to improve that process, but I'm not sure what their status is these days. – Hounshell Jan 19 '15 at 17:49
  • 3
    Hi @Hounshell, please remove the `connect()` call from the `onConnectionSuspended` in the [quickstart apps](https://github.com/googleplus/gplus-quickstart-android/blob/master/app/src/main/java/com/google/android/gms/plus/sample/quickstart/MainActivity.java) then, thanks. – JohnTube Jan 20 '15 at 23:42
  • 2
    Interestingly, I just filed a Pull Request for this myself today at https://github.com/googlesamples/android-play-location/pull/3 – Carlos P Mar 09 '15 at 12:56
  • 2
    While the `GoogleApiClient` _attempts_ to reconnect, for me it never succeeded. I had to use a `Handler` and delay a manual call to reconnect by `1000ms` in order to see `onConnected()` successfully note the connection. – matt--- Mar 24 '16 at 22:49
  • @Hounshell can you please respond on this question on a similar issue? http://stackoverflow.com/questions/36196816/what-is-the-googleapiclient-reconnect-policy-after-a-connection-gets-suspended – Muzikant Sep 11 '16 at 06:51
  • @Hounshell if the connection is suspended then reconnects, will FussedLocationProvider queries also recover, or do the queries need to be restarted? – MNDaveC Oct 31 '17 at 15:46
  • @Hounshell I am using FusedocationAPI. When My GPS is active then its working fine but My requirement is when I turn off GPS the onConnectionSuspended() dosen;t execute.. How can I hold that situation where we can trace when User turn off his GPS ? – coder_baba Dec 21 '17 at 06:44
1

The onConnected() doc says the follwing:

After calling connect(), this method will be invoked asynchronously when the connect request has successfully completed.

This implies that you have to call connect() otherwise onConnected() won't be called.

Ogen
  • 6,499
  • 7
  • 58
  • 124
  • It's true that we have to call connect after creating the GoogleApiClient. But based on @hounshell answer, we don't have to call connect again since it will be called automatically. – Raymond Lukanta Feb 25 '16 at 07:30