问题
I'm integrating Google Play Game Services in my game. Now I want to retrieve a list of achievements without starting the achievements intent.
I want the list behind that intent so that I can populate my own UI elements with this information. I'm not using the old GooglePlayServicesClient, I'm using the GoogleApiClient!
Thanks for your help ;)
回答1:
The code to retrieve a list of achievements can be found in this answer.
Below is the code snippet - see the linked answer for a full description:
public void loadAchievements() {
boolean fullLoad = false; // set to 'true' to reload all achievements (ignoring cache)
float waitTime = 60.0f; // seconds to wait for achievements to load before timing out
// load achievements
PendingResult p = Games.Achievements.load( playHelper.getApiClient(), fullLoad );
Achievements.LoadAchievementsResult r = (Achievements.LoadAchievementsResult)p.await( waitTime, TimeUnit.SECONDS );
int status = r.getStatus().getStatusCode();
if ( status != GamesStatusCodes.STATUS_OK ) {
r.release();
return; // Error Occured
}
// cache the loaded achievements
AchievementBuffer buf = r.getAchievements();
int bufSize = buf.getCount();
for ( int i = 0; i < bufSize; i++ ) {
Achievement ach = buf.get( i );
// here you now have access to the achievement's data
String id = ach.getAchievementId(); // the achievement ID string
boolean unlocked = ach.getState == Achievement.STATE_UNLOCKED; // is unlocked
boolean incremental = ach.getType() == Achievement.TYPE_INCREMENTAL; // is incremental
if ( incremental )
int steps = ach.getCurrentSteps(); // current incremental steps
}
buf.close();
r.release();
}
This code should be run in an AsyncTask
since it may take some time to complete while waiting for the achievements to load.
来源:https://stackoverflow.com/questions/25179827/google-play-game-services-get-achievements-list