5

I'm trying to add achievements to my game by using google play game services. The activity in my game that is responsible for giving the achievement already extends BaseGameActivity and it calls the beginUserInitiatedSignIn when it has to give the achievement, so the user must be signing in, but at the time I unlock the achievement for the user, I keep getting "java.lang.IllegalStateException: GoogleApiClient is not connected yet". Can anyone tell me what I am doing wrong? Here's the code responsible for unlocking the achievement(it's 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();
        }
    }

}
  • See [this post](http://stackoverflow.com/questions/24474986/android-google-play-games-services-connection-error-java-lang-illegalstateexc). – jfdoming Dec 08 '14 at 22:16
  • yea, I saw this post, but I don't want a sign-in button, I want my applicatio to try to sign in the user immediatly. Is it impossible? – Fabio Phillip Rocha Marques Dec 08 '14 at 22:19
  • Yeah, unfortunately you need to ask for a user's permission before you sign in, 'cause signing in means your app gets all sorts of new permissions. That's why Android forces you to show a dialog first. – jfdoming Dec 08 '14 at 22:28
  • As far as I know, there is no way, but if anyone else discovers one, they should clearly post it here. – jfdoming Dec 08 '14 at 22:29

1 Answers1

2

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

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

Clayton Wilkinson
  • 4,524
  • 1
  • 16
  • 25