Java.lang.IllegalStateException: GoogleApiClient not yet connected

I am trying to add achievements to my game using google game services. The activity in my game, which is responsible for providing achievements, already extends BaseGameActivity and calls beginUserInitiatedSignIn when it should give an achievement, so the user must log in, but at the time I unlock the achievement for the user, I keep receiving "java.lang .IllegalStateException: GoogleApiClient not yet connected. " Can someone tell me what I am doing wrong? Here is the code responsible for unlocking the achievement (it is in the class that extends BaseGameActivity from BaseGameUtils):

private void darConquistaDerrubouArvore(int numeroDeAcertos) { // start the asynchronous sign in flow mSignInClicked = true; mGoogleApiClient.connect(); if(numeroDeAcertos <= 40) { try { beginUserInitiatedSignIn(); Games.Achievements.unlock(gameHelper.getApiClient(), "CgkIs_27xcoSEAIQAQ"); Log.i("TelaModoCasual", "usuário não está logado"); this.onSignInFailed(); } catch(Exception exc) { exc.printStackTrace(); this.onSignInFailed(); } } } 
+5
source share
1 answer

Take a look at the basic example https://github.com/playgameservices/android-basic-samples/tree/master/BasicSamples/TypeANumber . The connection flow for the Google APIs is asynchronous, so you cannot unlock the achievement as it is. There is a callback onConnected (), which is called after the connection is established. In this method you can unlock achievements.

The documentation for the api client is at http://developer.android.com/google/auth/api-client.html

+2
source

All Articles