Search code examples
javaandroidgoogle-api-client

How to know when Google Play Games successfully connects?


I want to start an activity when Google Play Games connects successfully. I've tried starting the activity in the onConnected method, the onConnectionSuspended method, and even the onConnectionFailed. The ectivity wouldn't start. I then placed a breakpoint in the onConnected method and debugged my app. The thread wasn't suspended even though Google Play Games was connected succesfully. These are my methods to connect to Google Play Games.

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    if (connectionResult.hasResolution()){
        try{
            connectionResult.startResolutionForResult(this, 0);
        }catch (IntentSender.SendIntentException e){
            mGoogleApiClient.connect();
        }
    }
}

This is my code for the Google Api Client.

mGoogleApiClient = new GoogleApiClient.Builder(this).addOnConnectionFailedListener(this)
            .addApi(Plus.API, Plus.PlusOptions.builder().build()).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES).build();

Am I doing anything wrong? Am I missing anything? Thanks.


Solution

  • The culprit is here

    mGoogleApiClient = new GoogleApiClient.Builder(this).addOnConnectionFailedListener(this)
    

    You didn't set the ConnectionCallbacks. Here are your options:

    mGoogleApiClient = new GoogleApiClient.Builder(this, this, this)
    // or
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
    

    Reference