从Google Play游戏服务获得用户分数?

时间:2014-03-12 07:35:28

标签: android google-play-games

我是Android App开发的新手。我想从Google Play游戏服务中检索用户的分数,我正在使用以下代码获得高分,但我不知道它如何返回值以及如何保存它。

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

将其保存为int或string不起作用。

3 个答案:

答案 0 :(得分: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是从Google Play游戏服务获取的排行榜ID

答案 1 :(得分:6)

方法:

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

返回

PendingResult<Leaderboards.LoadPlayerScoreResult>

然后,您必须使用getScore()类的Leaderboards.LoadPlayerScoreResult方法来获取它。

请参阅这些链接......

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();
            }

        });

这就是你得分的方法。

答案 2 :(得分:3)

4年后,我在这种情况下遇到了类似的问题。有些东西已被弃用,而这些东西不再起作用。因此,对于那些想立即知道如何做的人,请在2018年...查看此答案-

首先,您必须通过以下方式获得LeaderBoardClient

mLeaderboardsClient = Games.getLeaderboardsClient(MainActivity.this, googleSignInAccount);

接下来您可以得分

mLeaderboardsClient.loadCurrentPlayerLeaderboardScore(getString(R.string.leaderboard_id), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC)
          .addOnSuccessListener(this, new OnSuccessListener<AnnotatedData<LeaderboardScore>>() {
              @Override
              public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
                  long score = 0L;
                  if (leaderboardScoreAnnotatedData != null) {
                      if (leaderboardScoreAnnotatedData.get() != null) {
                          score = leaderboardScoreAnnotatedData.get().getRawScore();
                          Toast.makeText(MainActivity.this, Long.toString(score), Toast.LENGTH_SHORT).show();
                          Log.d(TAG, "LeaderBoard: " + Long.toString(score));
                      } else {
                          Toast.makeText(MainActivity.this, "no data at .get()", Toast.LENGTH_SHORT).show();
                          Log.d(TAG, "LeaderBoard: .get() is null");
                      }
                  } else {
                      Toast.makeText(MainActivity.this, "no data...", Toast.LENGTH_SHORT).show();
                      Log.d(TAG, "LeaderBoard: " + Long.toString(score));
                  }
              }
          })
          .addOnFailureListener(new OnFailureListener() {
              @Override
              public void onFailure(@NonNull Exception e) {
                  Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
                  Log.d(TAG, "LeaderBoard: FAILURE");
              }
  });

.loadCurrentPlayerLeaderboardScore 的3个参数如下

  

排行榜的ID ,从中加载得分。

     

时间跨度以获取数据。有效值为TIME_SPAN_DAILY,TIME_SPAN_WEEKLY或TIME_SPAN_ALL_TIME。

     

排行榜收藏以获取分数。有效值为COLLECTION_PUBLIC或COLLECTION_SOCIAL。