【问题标题】:Android / Java: GoogleApiClient won't connectAndroid / Java:GoogleApiClient 无法连接
【发布时间】:2015-11-04 14:47:25
【问题描述】:

我使用 Android Studio 用 Ja​​va 编写了一个 Android 游戏。现在我想通过 GoogleApi 在线交换玩家的高分。所以我在onCreate 函数中初始化了一个GoogleApiClient:

googleApi = new GoogleApiClient.Builder(FullscreenActivity.this)
                .addApi(Games.API)
                .addOnConnectionFailedListener(this)
                .addConnectionCallbacks(this)
        .build();

其中googleApi 是公共GoogleApiClient 变量。 然后是:

@Override
    protected void onStart() {
        super.onStart();
        Log.e("Connected?", String.valueOf(googleApi.isConnected()));
        googleApi.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.d("ConnectionFailed", String.valueOf(result));
        if (result.hasResolution()) {
            try {
                // !!!
                result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (IntentSender.SendIntentException e) {
                googleApi.connect();
            }
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        if(!started){
            started = true;
            setContentView(new Game(this));
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        if(!started){
            started = true;
            this.setContentView(new Game(this));
        }
    }

onConnectionFailed(...) 的输出说:D/ConnectionFailed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{2b5bddee: android.os.BinderProxy@7d0328f}, message=null}

在我的手机上出现了 Google Play 游戏登录窗口,我登录了。然后出现了一个旋转的进度圈,然后它就消失了。 onConnected(...) 函数从未被调用。 添加/删除/编辑什么?

这很可能不是重复的,因为我没有找到其他几个问题的有效解决方案,内容相同。

【问题讨论】:

标签: java android google-play-services google-play-games


【解决方案1】:

在登录过程中,可以多次调用onConnectionFailed。你看过 GitHub 中的示例:https://github.com/playgameservices/android-basic-samples/tree/master/BasicSamples 吗?

在示例中 onConnectionFailed 被实现为:

 public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d(TAG, "onConnectionFailed");
        if (mIsResolving) {
            // The application is attempting to resolve this connection failure already.
            Log.d(TAG, "onConnectionFailed: already resolving");
            return;
        }

        if (mSignInClicked || mAutoStartSignIn) {
            mSignInClicked = false;
            mAutoStartSignIn = false;

            // Attempt to resolve the connection failure.
            Log.d(TAG, "onConnectionFailed: begin resolution.");
            mIsResolving = resolveConnectionFailure(this, mGoogleApiClient,
                    connectionResult, RC_SIGN_IN, getString(R.string.signin_other_error));
        }

        updateUI();
    }

resolveConnectionFailure 是:

public static boolean resolveConnectionFailure(Activity activity,
                                                   GoogleApiClient client, ConnectionResult result, int requestCode,
                                                   String fallbackErrorMessage) {

        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(activity, requestCode);
                return true;
            } catch (IntentSender.SendIntentException e) {
                // The intent was canceled before it was sent.  Return to the default
                // state and attempt to connect to get an updated ConnectionResult.
                client.connect();
                return false;
            }
        } else {
            // not resolvable... so show an error message
            int errorCode = result.getErrorCode();
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(errorCode,
                    activity, requestCode);
            if (dialog != null) {
                dialog.show();
            } else {
                // no built-in dialog: show the fallback error message
                showAlert(activity, fallbackErrorMessage);
            }
            return false;
        }
    }

【讨论】:

  • 谢谢 :) 这是我还没有看到的解决方案,让我们看看它是否有效 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-05
  • 2016-08-18
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 2014-05-02
相关资源
最近更新 更多