2015-11-04 31 views
0

我使用Android Studio在Java中編寫了Android遊戲。現在我想通過GoogleApi在線交換球員的高分。所以,我在onCreate函數初始化GoogleApiClientAndroid/Java: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: [email protected]}, message=null}

我的手機在谷歌玩遊戲登錄窗口出現了,並且我登錄然後旋轉進步圈子被顯示,並消失了。功能從來沒有被調用過。 要添加/刪除/編輯什麼?

這很可能不是重複的,因爲我沒有找到其他幾個問題的工作解決方案,內容相同。

+1

檢查谷歌播放服務的日誌https://developers.google.com/games/services/ android/logging – danieljohngomez

+0

我會先看看,謝謝。 – Unknown

回答

0

在登錄過程中,可能會有多次調用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; 
     } 
    } 
+0

謝謝:)這是我還沒有看到的解決方案,讓我們看看它是否工作:) – Unknown