簡體   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