【问题标题】:How to know when Google Play Games successfully connects?如何知道 Google Play 游戏何时成功连接?
【发布时间】:2017-07-11 17:01:13
【问题描述】:

我想在 Google Play 游戏连接成功后开始一项活动。我尝试在 onConnected 方法、onConnectionSuspended 方法甚至 onConnectionFailed 中启动活动。活动不会开始。然后我在 onConnected 方法中放置了一个断点并调试了我的应用程序。即使 Google Play 游戏连接成功,该线程也没有暂停。 这些是我连接到 Google Play 游戏的方法。

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    if (connectionResult.hasResolution()){
        try{
            connectionResult.startResolutionForResult(this, 0);
        }catch (IntentSender.SendIntentException e){
            mGoogleApiClient.connect();
        }
    }
}

这是我的 Google Api 客户端代码。

mGoogleApiClient = new GoogleApiClient.Builder(this).addOnConnectionFailedListener(this)
            .addApi(Plus.API, Plus.PlusOptions.builder().build()).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES).build();

我做错了吗?我错过了什么吗?谢谢。

【问题讨论】:

  • The thread wasn't suspended even though Google Play Games was connected succesfully 你怎么知道它连接了?我认为这就是问题所在。
  • 嗯,我知道它已连接,但 android 没有。我希望 android 知道它已连接,以便我可以开始活动。

标签: java android google-api-client


【解决方案1】:

罪魁祸首在这里

mGoogleApiClient = new GoogleApiClient.Builder(this).addOnConnectionFailedListener(this)

您没有设置ConnectionCallbacks。以下是您的选择:

mGoogleApiClient = new GoogleApiClient.Builder(this, this, this)
// or
mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)

Reference

【讨论】:

  • 谢谢,成功了!我会在 3 分钟内接受你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 2013-09-24
  • 2016-05-15
  • 2015-01-30
  • 1970-01-01
相关资源
最近更新 更多