2016-11-22 159 views
0

你好我有試圖在Android應用程序中使用LeaderBoards。應用程序需要谷歌遊戲安裝應用程序崩潰時播放

____________________________EDIT________________________________________________

好了,現在我想請點擊此鏈接Guide

我創建測試新的項目。我有像Giude一樣的代碼。

如果希望登錄顯示爲Google遊戲下載彈出窗口...好吧,下載它。

但如果我再次打開應用程序應用程序崩潰。我不知道爲什麼。 (我在電話號碼模擬器中啓動應用程序)

您有什麼想法嗎? [我有一個新的項目只有來自指南的代碼。 + basegameUtils]

public class MainActivity extends Activity implements 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, View.OnClickListener { 

    private static int RC_SIGN_IN = 9001; 

    private boolean mResolvingConnectionFailure = false; 
    private boolean mAutoStartSignInflow = true; 
    private boolean mSignInClicked = false; 
    private boolean mAutoStartSignInFlow=false; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     try { 

      findViewById(R.id.sign_in_button).setOnClickListener(this); 
      findViewById(R.id.sign_out_button).setOnClickListener(this); 


      // Create the Google Api Client with access to the Play Games services 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(Games.API).addScope(Games.SCOPE_GAMES) 
        // add other APIs and scopes here as needed 
        .build(); 

     }catch (Exception e) 
     { 
      Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     // mGoogleApiClient.connect(); 

     if (!mInSignInFlow && !mExplicitSignOut) { 
      // auto sign in 
      mGoogleApiClient.connect(); 
     } 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     mGoogleApiClient.disconnect(); 
    } 


    @Override 
    public void onConnected(Bundle connectionHint) { 
     // The player is signed in. Hide the sign-in button and allow the 
     // player to proceed. 

     // show sign-out button, hide the sign-in button 
     findViewById(R.id.sign_in_button).setVisibility(View.GONE); 
     findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE); 

     // (your code here: update UI, enable functionality that depends on sign in, etc) 

    } 


    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     if (mResolvingConnectionFailure) { 
      // already resolving 
      return; 
     } 

     // if the sign-in button was clicked or if auto sign-in is enabled, 
     // launch the sign-in flow 
     if (mSignInClicked || mAutoStartSignInFlow) { 
      mAutoStartSignInFlow = false; 
      mSignInClicked = false; 
      mResolvingConnectionFailure = true; 

      // Attempt to resolve the connection failure using BaseGameUtils. 
      // The R.string.signin_other_error value should reference a generic 
      // error string in your strings.xml file, such as "There was 
      // an issue with sign-in, please try again later." 
      if (!BaseGameUtils.resolveConnectionFailure(this, 
        mGoogleApiClient, connectionResult, 
        RC_SIGN_IN, String.valueOf(R.string.signin_other_error))) { 
       mResolvingConnectionFailure = false; 
      } 
     } 

     // Put code here to display the sign-in button 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     // Attempt to reconnect 
     mGoogleApiClient.connect(); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, 
            Intent intent) { 
     if (requestCode == RC_SIGN_IN) { 
      mSignInClicked = false; 
      mResolvingConnectionFailure = false; 
      if (resultCode == RESULT_OK) { 
       mGoogleApiClient.connect(); 
      } else { 
       // Bring up an error dialog to alert the user that sign-in 
       // failed. The R.string.signin_failure should reference an error 
       // string in your strings.xml file that tells the user they 
       // could not be signed in, such as "Unable to sign in." 
       BaseGameUtils.showActivityResultError(this, 
         requestCode, resultCode, R.string.signin_failure); 
      } 
     } 
    } 


    // Call when the sign-in button is clicked 
    private void signInClicked() { 
     mSignInClicked = true; 
     mGoogleApiClient.connect(); 
    } 

    // Call when the sign-out button is clicked 
    private void signOutclicked() { 
     mSignInClicked = false; 
     Games.signOut(mGoogleApiClient); 
    } 




    @Override 
    public void onClick(View view) { 
     if (view.getId() == R.id.sign_in_button) { 
      // start the asynchronous sign in flow 
      mSignInClicked = true; 
      mGoogleApiClient.connect(); 
     } 
     else if (view.getId() == R.id.sign_out_button) { 
      // sign out. 
      mSignInClicked = false; 
      Games.signOut(mGoogleApiClient); 

      // show sign-in button, hide the sign-out button 
      findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE); 
      findViewById(R.id.sign_out_button).setVisibility(View.GONE); 
     } 

     if (view.getId() == R.id.sign_out_button) { 
      // user explicitly signed out, so turn off auto sign in 
      mExplicitSignOut = true; 
      if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { 
       Games.signOut(mGoogleApiClient); 
       mGoogleApiClient.disconnect(); 
      } 
     } 
    } 


    boolean mExplicitSignOut = false; 
    boolean mInSignInFlow = false; // set to true when you're in the middle of the 
    // sign in flow, to know you should not attempt 
    // to connect in onStart() 
    GoogleApiClient mGoogleApiClient; // initialized in onCreate 


} 
+0

排行榜不需要Google+登錄。什麼給了你這個想法?谷歌玩遊戲有自己的登錄。 – ianhanniballake

+0

所以現在如何實現排行榜?我zhing,所以我需要谷歌+並在leaderbard有谷歌的名稱和分數..請給出建議。或示例代碼..謝謝。對於排行榜(不管是什麼,沒有google +) – trip03

+0

您是否嘗試過[Google Play遊戲登錄指南](https://developers.google.com/games/services/training/signin)? – ianhanniballake

回答

0

我發現我的錯誤..

我是沒有這個

<meta-data android:name="com.google.android.gms.games.APP_ID" 
     android:value="@strings/app_id" /> 
<application></application>

,這是一個問題。

相關問題