Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle disconnect from Google Game Services?

Tags:

android

I use Google Game Services for leaderboards. Showing it like this:

static public void showLeaderboard(String lid)
{
  if (isLogined() == 1)
  {
    Log.i(TAG, "Showing leaderboard...");
    Intent intent = Games.Leaderboards.getLeaderboardIntent(mClient, lid);
    mApp.startActivityForResult(intent, 1);
  }
}

static public int isLogined()
{
  if (mClient != null && mClient.isConnected())
    return 1;
  return 0;
}

But when I open leaderboards and logout from Google Game Services with Google UI (Action Overflow menu icon -> Settings -> Sign out) I keep having my isLogined() == 1. So, when I call showLeaderboard() second time - game falls with exception:

java.lang.SecurityException: Not signed in when calling API

GoogleApiClient has callbacks for connection but not for disconnection. How can I handle sing out from GGS with google UI?

like image 483
Kharkov Alex Avatar asked Feb 12 '23 21:02

Kharkov Alex


1 Answers

In order to keep everything synced up you MUST implement onActivityResult properly.

This should look something as follows:

@Override
protected void onActivityResult(int request, int response, Intent data) {

   // check for "inconsistent state"
   if ( responseCode == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED && requestCode == <your_request_code_here> )  {  
      // force a disconnect to sync up state, ensuring that mClient reports "not connected"
      mClient.disconnect();
   }
}

NOTE: just make sure to replace <your_request_code_here> in the code with the request code you used (which is just 1 in your example). You may need to check for multiple request codes if you use achievements as well.

like image 102
free3dom Avatar answered Feb 23 '23 03:02

free3dom