简体   繁体   中英

Google Play Game Services Sign Out Callback

I am trying to implement Google Play Game Services to my application. I managed to get user to sign in automaticlly on launch, and show leaderboards, upload player's score.

I want to handle player sign out as well. Curently, player can sign out from Google Play Services Leaderboard window. My question is how to implement an interface to detect the user logout from Services window.

http://i.imgur.com/FlAVSbF.jpg

There is no callback for signing out that is part of the API. You can check the GoogleAPIClient.isConnected() , or if using the C++ SDK, call gpg::GameServices::IsAuthorized()

So when you show the leaderboard, you can check in onActivityResult to see if they are connected. If they are not, it most likely means they signed out from the Settings menu.

public void showLeaderboard()
{
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mGoogleApiClient,
        LEADERBOARD_ID), REQUEST_LEADERBOARD);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == REQUEST_LEADERBOARD) {
      // check if user signed out
      mExplicitSignOut = mGoogleApiClient != null && !mGoogleApiClient.isConnected();
  }
  // rest of onActivityResult... 
}

Let me share another solution, which seems more viable in 2019:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RESULT_CODE_GOOGLE_PLAY_GAMES) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            GoogleSignInAccount account = task.getResult(ApiException.class);
            // User is signed in
        } catch (ApiException apiException) {
            // User is signed out
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM