问题
loadCurrentPlayerLeaderboardScore
method sometimes returns the value from my leaderboards and other times there is no result, so 999 is returned for LB_SCORE
and LB_RANK
. I am connected to google play games service, I am able to view the leaderboards using the startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mClient, leaderboardId), 1);
so I can confirm there are valid scores. How can I make calling loadCurrentPlayerLeaderboardScore
more reliable and consistent?
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;
}
回答1:
I faced similar issues, and I see that other post confirms my suspicions that making too many calls will result in network error. So my suggestions is that you are abusing play service too much.
来源:https://stackoverflow.com/questions/40026704/android-loadcurrentplayerleaderboardscore-not-always-returning-result-values