【问题标题】:onConnectionSuspended. How to test? When will this code be run?onConnectionSuspended。如何测试?这段代码什么时候运行?
【发布时间】:2015-02-05 15:41:06
【问题描述】:
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks {

    @Override
    public void onConnectionSuspended(int i) {
        Log.d(TAG, "onConnectionSuspended() called. Trying to reconnect.");
        sendToast("onConnectionSuspended() called. Trying to reconnect.");
        mGoogleApiClient.connect();
    }

    [...]
}

我已阅读文档: https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.ConnectionCallbacks.html

根据我读到的内容,我做了一个简单的测试,我在一个快速游戏中连接了两个对等点。我禁用了其中一个上的 WiFi 连接。我以为我会看到为 onConnectionSuspended 干杯。有没有办法让 toast 出现以进行测试?

谢谢

【问题讨论】:

  • 请确保您的代码缩进正确。另外,RoomStatusUpdateListenerRoomUpdateListener 等是什么?这些与您的问题有关吗?如果没有,我建议您删除它们以避免混淆。

标签: android google-play-games


【解决方案1】:

onConnectionSuspended 在您的应用与 Google Play 服务包(不一定是互联网)断开连接时被调用。例如,当您转到 设置 > 应用程序 > Google Play 服务 > 强制停止 时,就会调用回调。另一个示例是当您卸载 Google Play 服务时。几秒钟后,您会收到onConnectionSuspended,然后是onConnectionFailed(因为重新连接尝试会失败)。

也不要从onConnectionSuspended(...) 调用mGoogleApiClient.connect()。自动处理重新连接。

【讨论】:

  • 我不确定您在官方 Google 快速入门应用程序中所做的最后陈述。 source
  • 我注意到,根据我的经验,它是自动处理的。
  • 根据代码作者stackoverflow.com/a/26147518/252080,自动重连。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2014-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
相关资源
最近更新 更多