注销后 Android API IsConnected 返回 TRUE

标签 android xamarin google-play-services monogame google-play-games

我正在使用 Google Play 游戏服务和 Xamarin 为 Android 开发游戏。我正在使用 Genymotion Android 模拟器进行测试。我遇到了一个似乎是 Google Play 或 Xamarin 实现中的错误的问题。

如果我退出 Google 帐户,对 IGoogleApiClient.IsConnected() 的调用将继续返回 true(即使我已经显然刚刚退出)。如果我随后尝试使用该 API 对象,我将得到如下异常:

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

例如,如果在注销后执行以下代码,则会导致上述异常:

public void StartNewMatch()
{
    if (!mGoogleApiClient.IsConnected)
    {
        return;
    }

    Intent intent = GamesClass.TurnBasedMultiplayer.GetSelectOpponentsIntent(mGoogleApiClient, 1, 1, true);
    StartActivityForResult(intent, RC_SELECT_PLAYERS);
}

我正在 Google Play 游戏收件箱(匹配选择器)中注销;如下图所示。

有人遇到过这个吗?我错过了什么吗?有任何解决方法吗?

注意:只有通过 Google 的用户界面注销时才会出现这种情况。如果我使用 mGoogleApiClient.Disconnect() 之类的方法手动注销用户,则不会出现此问题发生; mGoogleApiClient.IsConnected() 现在返回 false(如预期)。

enter image description here

enter image description here

enter image description here

最佳答案

为了保持登录状态同步,您必须正确实现 onActivityResult。

这应该看起来像下面这样:

注意:这是 Java 代码,我不确定使用 Xamarin 时它的外观如何,但希望您应该能够弄明白:)

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

注意:只需确保将代码替换为您使用的请求代码。您可能还需要检查多个请求代码。

关于注销后 Android API IsConnected 返回 TRUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26902935/

相关文章:

android - 在android中只显示最后一个ArrayList

xamarin - Xamarin Forms iOS应用程序在启动时崩溃

android - 不在 android 中使用 google play 服务显示 admob 横幅广告

android - setConnectivityMode Skmaps 不工作?

android - 在textview android上捏缩放

javascript - Xamarin Android Webview Javascript

android - 具有多个帐户的应用内购买

android - 是否可以在不添加播放服务广告依赖的情况下获取 android 广告 ID?

android - 有没有办法只为 android 4.0.3 禁用硬件加速?

c# - 将焦点设置到 Xamarin.Forms 中的条目