【问题标题】:GoogleApiClient Games.API connect errorGoogleApiClient Games.API 连接错误
【发布时间】:2017-02-25 00:24:02
【问题描述】:

我想使用 Play 游戏服务,所以我使用带有 Games.API 的 GoogleApiClient 并调用方法 connect()。但是它总是在 onConnectionFailed() 中给出错误 RESOLUTION_REQUIRED。

public class MainActivity extends Activity implements
    ConnectionCallbacks,
    OnConnectionFailedListener {
GoogleApiClient googleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();    }

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    //ConnectionResult{statusCode=RESOLUTION_REQUIRED, resolution=PendingIntent{dd739ef: android.os.BinderProxy@624acfc}, message=null}
}

但如果我使用 Auth.API 而不是 Games.API,则连接成功并调用方法 onConnected()。

public class MainActivity extends Activity implements
    ConnectionCallbacks,
    OnConnectionFailedListener {
GoogleApiClient googleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Auth.GOOGLE_SIGN_IN_API)
            .build();
}

@Override
public void onConnected(Bundle connectionHint) {
    //Success
}

我用的是模拟器,模拟器中播放服务的版本是10.0,所以我在gradle build中也设置了10.0版本

compile 'com.google.android.gms:play-services-games:10.0.0'
compile 'com.google.android.gms:play-services-auth:10.0.0'

我在这里Google Play fails to connect, statusCode=RESOLUTION_REQUIRED发现了同样的问题,但是作者没有得到任何答案。

【问题讨论】:

    标签: android google-play-services google-api-client


    【解决方案1】:

    基于ConnectionResult,错误代码RESOLUTION_REQUIRED

    完成连接需要某种形式的解决方案。可以使用startResolutionForResult(Activity, int) 开始解决方案。如果返回的结果是RESULT_OK,那么进一步的连接尝试应该要么完成,要么继续处理下一个需要解决的问题。

    除此之外,您可能还想检查应如何处理连接失败,如Manually managed connections 中所述。如前所述,

    当您的应用收到对onConnectionFailed()) 回调的调用时,您应该在提供的ConnectionResult 对象上调用hasResolution()。如果它返回 true,您的应用程序可以通过在 ConnectionResult 对象上调用 startResolutionForResult() 来请求用户立即采取措施解决错误。在这种情况下,startResolutionForResult() 方法的行为与 startActivityForResult() 相同,并启动适合于上下文的 Activity,以帮助用户解决错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多