【问题标题】:How to handle disconnect from Google Game Services?如何处理与 Google 游戏服务的断开连接?
【发布时间】:2014-10-02 14:09:29
【问题描述】:

我将 Google 游戏服务用于排行榜。显示如下:

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;
}

但是,当我使用 Google UI(操作溢出菜单图标 -> 设置 -> 退出)打开排行榜并从 Google 游戏服务注销时,我的 isLogined() == 1 一直存在。所以,当我第二次调用 showLeaderboard()时间 - 游戏异常失败:

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

GoogleApiClient 有连接回调,但没有断开连接。 如何使用 google UI 处理 GGS 的唱歌?

【问题讨论】:

  • Nitpick:不要返回 01 来表示失败或成功,而应返回 boolean(无论如何,这是 Java 的做法)。
  • 我们有几个平台,对于常用功能,使用int 而不是boolean(例如,objc 中没有boolean)以便于移植。
  • (只是说)。无论如何,这段代码对我来说看起来不错。您是否在物理设备上进行测试?也许应该下载一个使用 Google Play 游戏的示例项目并自己运行它......然后如果示例项目正常运行,您可以将其与您当前的项目进行比较,并尝试找出导致中断的差异。

标签: android


【解决方案1】:

为了使所有内容保持同步,您必须正确实施 onActivityResult

这应该如下所示:

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

注意:只需确保将代码中的 &lt;your_request_code_here&gt; 替换为您使用的请求代码(在您的示例中就是 1)。如果您也使用成就,则可能需要检查多个请求代码。

【讨论】:

    【解决方案2】:

    通过添加.disconnect.connect,只需trycatchshowLeaderbord 方法也可以正常工作,然后在catch 正文中重试

    【讨论】:

      猜你喜欢
      • 2013-09-24
      • 2017-06-13
      • 1970-01-01
      • 2013-08-31
      • 2016-07-08
      • 1970-01-01
      • 2012-10-18
      • 2013-06-16
      • 1970-01-01
      相关资源
      最近更新 更多