2014-03-12 69 views
4

我是Android App開發新手。我想從Google Play遊戲服務中檢索用戶的分數,並使用以下代碼獲得高分,但我不知道它如何返回值以及如何保存它。從Google Play遊戲服務獲取用戶分數?

Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(R.string.highscore), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC); 

將其保存爲int或字符串不起作用。

回答

6

的方法:

loadCurrentPlayerLeaderboardScore (GoogleApiClient apiClient, String leaderboardId, int span, int leaderboardCollection)

回報

PendingResult<Leaderboards.LoadPlayerScoreResult>

然後,你必須使用​​類的getScore()方法得到它。

請參閱以下鏈接...

The loadCurrentPlayerLeaderboardScore method

The LoadPlayerScoreResult in the PendingResult

編輯:這裏是你如何使用它。

Games.Leaderboards.loadCurrentPlayerLeaderboardScore().setResultCallback(new ResultCallback<LoadPlayerScoreResult>() { 

      @Override 
      public void onResult(LoadPlayerScoreResult arg0) { 
       LeaderboardScore c = arg0.getScore(); 
      } 

     }); 

這就是你如何得分。

+0

@SudeepAcharya我要去的地方的編輯,請參閱我的樣本代碼 – Ogen

+0

@SudeepAcharya添加參數以匹配方法簽名:loadCurrentPlayerLeaderboardScore(GoogleApiClient apiClient,String leaderboardId,int span,int leaderboardCollection) – Ogen

+0

@SudeepAcharya使用c.getRawScore(),它會給你一個long類型的值。查看此鏈接以獲取評分的其他方式:http://developer.android.com/reference/com/google/android/gms/games/leaderboard/LeaderboardScore.html – Ogen

7

爲loadCurrentPlayerLeaderboardScore完整的參數是像下面

 Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), 
      getString(R.string.word_attack_leaderboard), 
      LeaderboardVariant.TIME_SPAN_ALL_TIME, 
      LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
      new ResultCallback<LoadPlayerScoreResult>() { 

       @Override 
       public void onResult(LoadPlayerScoreResult arg0) { 
        LeaderboardScore c = arg0.getScore(); 
        long score = c.getRawScore(); 
       } 
      } 

R.string.word_attack_leaderboard是排行榜的ID從谷歌獲得玩遊戲服務

相關問題