How to deal with disconnecting from Google Game Services?

I use Google Game Services for leaders. 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 the leaderboards and exit the Google Game Services using the Google user interface (menu icon "Overflow" → "Settings" → "Exit"), I save my isLogined () == 1. So, when I call showLeaderboard ( ) the second time - the game crashes with the exception of:

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

GoogleApiClient has callbacks to connect, but not to disconnect. How can I handle singing from GGS using the Google UI?

+4
source share
2 answers

To keep everything in sync, you MUST implement onActivityResultcorrectly.

:

@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();
   }
}

. <your_request_code_here> , ( 1 ). , .

+8

try catch showLeaderbord , .disconnect .connect, catch body

0

All Articles