问题
I am developing a game for Android using Google Play Game Services, using Xamarin. I am doing my testing using a Genymotion Android Emulator. I have run into an issue that appears to be a bug in either Google Play or Xamarin's implementation.
If I sign out of a Google account, calls to the IGoogleApiClient.IsConnected() continue to return true (even though I have clearly just signed out). If I then attempt to use that API object, I will get exceptions like:
java.lang.SecurityException: Not signed in when calling API
For example, the follow code results in the above exception if executed after signing out:
public void StartNewMatch()
{
if (!mGoogleApiClient.IsConnected)
{
return;
}
Intent intent = GamesClass.TurnBasedMultiplayer.GetSelectOpponentsIntent(mGoogleApiClient, 1, 1, true);
StartActivityForResult(intent, RC_SELECT_PLAYERS);
}
I am signing out in the Google Play Games Inbox (match picker); as shown in the images below.
Anyone run into this before? Am I missing something? Got any work-arounds?
Note: This only occurs if signing out through Google's UI. If I manually sign the user out, with something like mGoogleApiClient.Disconnect()
, the issue does not occur; mGoogleApiClient.IsConnected()
now returns false (as expected).
回答1:
In order to keep signed-in state synced up you MUST implement onActivityResult properly.
This should look something as follows:
NOTE: this is java code, I am not sure how this will look exactly using Xamarin, but hopefully you should be able to figure it out :)
@Override
protected void onActivityResult(int requestCode, int responseCode, 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"
mGoogleApiClient.disconnect();
}
}
NOTE: just make sure to replace in the code with the request code you used. You may need to check for multiple request codes too.
回答2:
If you are using gameHelper classes from BaseGameUtils library(it is easier to use), you may modify the above code to this:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
gameHelper.onActivityResult(requestCode, resultCode, data);
if (resultCode == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED){
// force a disconnect to sync up state, ensuring that mClient reports "not connected"
gameHelper.getApiClient().disconnect();
}
}
来源:https://stackoverflow.com/questions/26902935/android-api-isconnected-returning-true-after-signing-out