1

我做了谷歌的API客戶端使用此代碼不能登錄到Google Play遊戲服務時,應用程序安裝

GoogleSignInOptions options = new GoogleSignInOptions 
      .Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) 
      .requestServerAuthCode(SERVER_CLIENT_ID) 
      .build(); 

mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this)      
       .addApi(Auth.GOOGLE_SIGN_IN_API, options) 
       .addApi(Games.API)     
       .build(); 

,並連接使用此代碼服務

mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL); 

連接成功但我不能得到serverauthcode,因爲 mGoogleApiClient.hasConnectedApi(Games.API)返回假 沒有任何警告或錯誤

@Override 
public void onConnected(Bundle bundle) { 

    if (mGoogleApiClient.hasConnectedApi(Games.API)) { 
     Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient).setResultCallback(
       new ResultCallback<GoogleSignInResult>() { 
        @Override 
        public void onResult(GoogleSignInResult googleSignInResult) { 

         // In this case, we are sure the result is a success. 
         GoogleSignInAccount signInAccount = googleSignInResult.getSignInAccount();       
         mAccountAuthCode = signInAccount.getServerAuthCode();  

         Player player = Games.Players.getCurrentPlayer(mGoogleApiClient); 
         mAccountId = player.getPlayerId(); 
         Log.i(MYTAG, "login success");   


        } 
       } 
     ); 
    } 
    else 
    {   
     Log.i(MYTAG, "connected but no Games.API Why!");    
    } 

奇怪的是,如果我只是斷開並重新連接, mGoogleApiClient.hasConnectedApi(Games.API)返回true,所有的事情都很好。

此外,如果我剛剛重新啓動應用程序 mGoogleApiClient.hasConnectedApi(Games.API)返回true,所有的事情都很好。

我想知道爲什麼我無法連接谷歌玩遊戲服務API第一次嘗試。

我使用的是最新的谷歌遊戲服務SDK(10.2.4)

任何意見,將不勝感激

+0

嘗試禁用即時運行。轉到設置並取消選中啓用即時運行,然後啓動應用程序。 – Sallu

+0

Sallu //是即時運行Android Studio功能嗎?我使用gradle直接構建我的apk並在控制檯上使用adb進行部署。 – gguo

+0

是的。轉至文件 - >設置 - >構建,執行,部署 - >即時運行 - >取消選中第一個複選框 – Sallu

回答

0

你可以參考這個thread。嘗試重新啓動您的設備。如果這沒有幫助,請清除緩存。如果問題持續存在,您還可以嘗試卸載並重新安裝Play商店應用更新。

對於mGoogleApiClient.hasConnectedApi(Games.API)返回false,您可以檢查此鏈接:GoogleApiClient.isConnected() return always false

你需要一對夫婦回調偵聽器添加到Builder(或GoogleApiClient)。最有可能發生的情況是登錄失敗,因爲您還沒有權限。您需要處理該故障,以便用戶可以授權您的應用程序。

更多閱讀:http://www.androidpolice.com/2014/02/14/for-developers-google-play-services-4-2-includes-new-client-api-model-consolidates-connections-under-one-object/

希望這有助於!

+0

在我的情況下,mGoogleApiClient.isConnected()返回true。只有mGoogleApiClient.hasConnectedApi(Games.API)返回false。並且onConnectionFailedListener永遠不會被調用。 – gguo

相關問題