繁体   English   中英

Android / Java:GoogleApiClient无法连接

[英]Android / Java: GoogleApiClient won't connect

我使用Android Studio用Java编写了一个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(...)函数。 添加/删除/编辑什么?

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

在登录过程中,可以多次调用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;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM