islander_zero
islander_zero

Reputation: 4062

Detect Sign Out from Google Play UI

My game is currently using Sign-In and Sign-out buttons from the menu in order to use Google Play leaderboards/Achievements. Unfortunately the user can also sign out from the Google Play UI but GameHelper.isSignedIn() is still returning true when they do this through Google's UI. When user tries to check leaderboard or achievement after user signs out this way, the game crashes.

Does anyone know an updated way to check if user signs out through UI? I say updated as I've seen a few threads in stackoverflow that does not work.

Upvotes: 4

Views: 1249

Answers (2)

n1m1
n1m1

Reputation: 979

I just followed the https://developers.google.com/games/services/training/signin and everything works fine. It is using

boolean mExplicitSignOut = false;
boolean mInSignInFlow = false; // set to true when you're in the middle of the
                           // sign in flow, to know you should not attempt
                           // to connect in onStart()
GoogleApiClient mGoogleApiClient;  // initialized in onCreate

@Override
protected void onStart() {
   super.onStart();
   if (!mInSignInFlow && !mExplicitSignOut) {
    // auto sign in
    mGoogleApiClient.connect();
   }
}

@Override
public void onClick (View view) {
if (view.getId() == R.id.sign_out_button) {
    // user explicitly signed out, so turn off auto sign in
    mExplicitSignOut = true;
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        Games.signOut(mGoogleApiClient);
        mGoogleApiClient.disconnect();
    }
}
}

Upvotes: 1

user682701
user682701

Reputation: 1449

I create a achievement named Sign in Play Games, and try to unlock it on onSingIn().

@Override
public boolean unlockAchievements() {
    boolean r = true;

    if (gameHelper.isSignedIn()){
        try{
            Games.Achievements.unlock(gameHelper.getApiClient(), getString(R.string.achievement_sign_in_play_games));
        }
        catch(Exception ex){
            r = false; 
        }
        finally{
        }
    }
    else{
        r = false;
    }

    return r;
}

On resize event of Screen where is my login button, I implement this code:

@Override
    public void resize(int width, int height) {

        //...

        if(game.gameHelper.isSignedIn()){
            if (!game.gameHelper.unlockAchievements()){
                game.gameHelper.forceSignOut();
            }
        }

    }

The forceSignOut() was implement on GameHelper class

public void forceSignOut() {
    if (mGoogleApiClient != null){
        mGoogleApiClient.disconnect();
    }
}

And finally in BaseGameActivity:

protected void forceSignOut(){
    mHelper.forceSignOut();
}

Don't forget to implement your GameServiceInterface:

public void forceSignOut();

Upvotes: 0

Related Questions