Android loadCurrentPlayerLeaderboardScore 并不总是返回结果值
Android loadCurrentPlayerLeaderboardScore not always returning result values
loadCurrentPlayerLeaderboardScore
方法有时 returns 来自我的排行榜的值,有时没有结果,因此 LB_SCORE
和 LB_RANK
返回 999。我已连接到 google 玩游戏服务,我可以使用 startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mClient, leaderboardId), 1);
查看排行榜,因此我可以确认分数有效。我怎样才能使调用 loadCurrentPlayerLeaderboardScore
更可靠和一致?
public void populateLeaderboards(){
String[] leaderboardIdArray = getResources().getStringArray(R.array.leaderboard_id_array);
String[] leaderboardNameArray = getResources().getStringArray(R.array.leaderboard_name_array);
leaderboardList = new ArrayList<LeaderboardItem>();
leaderboardList.clear();
for (int i = 0; i < leaderboardIdArray.length; i++) {
Log.d("leaderboardIdArray", leaderboardIdArray[i]);
getLeaderboardData(leaderboardIdArray[i],leaderboardNameArray[i]);
LeaderboardItem(leaderboardIdArray[i], LB_NAME, String.valueOf(LB_SCORE), String.valueOf(LB_RANK));
}
}
private void getLeaderboardData(final String leaderboardId,final String leaderboardName) {
Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mClient, leaderboardId, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
//Games.Leaderboards.loadPlayerCenteredScores(mClient, leaderboardId, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, 1, true).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
@Override
public void onResult(final Leaderboards.LoadPlayerScoreResult scoreResult) {
if (isScoreResultValid(scoreResult)) {
// here you can get the score like this
LB_SCORE = scoreResult.getScore().getRawScore();
LB_RANK = scoreResult.getScore().getRank();
} else {
LB_SCORE = 999;
LB_RANK = 999;
}
LeaderboardItem leaderboardItem = new LeaderboardItem(leaderboardId, leaderboardName, String.valueOf(LB_SCORE), String.valueOf(LB_RANK));
leaderboardList.add(leaderboardItem);
}
});
}
private boolean isScoreResultValid(final Leaderboards.LoadPlayerScoreResult scoreResult) {
return scoreResult != null && GamesStatusCodes.STATUS_OK == scoreResult.getStatus().getStatusCode() && scoreResult.getScore() != null;
}
我遇到了类似的问题,我看到 other post confirms 我怀疑调用太多会导致网络错误。所以我的建议是你过度滥用播放服务。
loadCurrentPlayerLeaderboardScore
方法有时 returns 来自我的排行榜的值,有时没有结果,因此 LB_SCORE
和 LB_RANK
返回 999。我已连接到 google 玩游戏服务,我可以使用 startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mClient, leaderboardId), 1);
查看排行榜,因此我可以确认分数有效。我怎样才能使调用 loadCurrentPlayerLeaderboardScore
更可靠和一致?
public void populateLeaderboards(){
String[] leaderboardIdArray = getResources().getStringArray(R.array.leaderboard_id_array);
String[] leaderboardNameArray = getResources().getStringArray(R.array.leaderboard_name_array);
leaderboardList = new ArrayList<LeaderboardItem>();
leaderboardList.clear();
for (int i = 0; i < leaderboardIdArray.length; i++) {
Log.d("leaderboardIdArray", leaderboardIdArray[i]);
getLeaderboardData(leaderboardIdArray[i],leaderboardNameArray[i]);
LeaderboardItem(leaderboardIdArray[i], LB_NAME, String.valueOf(LB_SCORE), String.valueOf(LB_RANK));
}
}
private void getLeaderboardData(final String leaderboardId,final String leaderboardName) {
Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mClient, leaderboardId, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
//Games.Leaderboards.loadPlayerCenteredScores(mClient, leaderboardId, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, 1, true).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
@Override
public void onResult(final Leaderboards.LoadPlayerScoreResult scoreResult) {
if (isScoreResultValid(scoreResult)) {
// here you can get the score like this
LB_SCORE = scoreResult.getScore().getRawScore();
LB_RANK = scoreResult.getScore().getRank();
} else {
LB_SCORE = 999;
LB_RANK = 999;
}
LeaderboardItem leaderboardItem = new LeaderboardItem(leaderboardId, leaderboardName, String.valueOf(LB_SCORE), String.valueOf(LB_RANK));
leaderboardList.add(leaderboardItem);
}
});
}
private boolean isScoreResultValid(final Leaderboards.LoadPlayerScoreResult scoreResult) {
return scoreResult != null && GamesStatusCodes.STATUS_OK == scoreResult.getStatus().getStatusCode() && scoreResult.getScore() != null;
}
我遇到了类似的问题,我看到 other post confirms 我怀疑调用太多会导致网络错误。所以我的建议是你过度滥用播放服务。