Reputation: 115
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.
https://i.sstatic.net/Kfmg7.jpg
Upvotes: 3
Views: 935
Reputation: 4572
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...
}
Upvotes: 1
Reputation: 6961
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
}
}
}
Upvotes: 0