问题
I have an Android game and I'd like to play a sound whenever a players unlocks an achievement, but I can't seem to find a way to know when the user unlocks the achievement, since most of my achievements are incremental I have no way to code it on the client side, the only way would be some callback called when the Google API shows the achievement box, but I can't find any of those on the documentation... Only thing i found was this: Integrating Google Play services achievements and android notifications
@Override
public void onAchievementUnlocked(final String id)
{
//
}
but I have no idea how to implement that or how to call that method and I had no luck on Google
回答1:
Use the following code:
Games.Achievements.incrementImmediate(GoogleApiClient apiClient, String id, int numSteps).setResultCallback(new ResultCallback<Achievements.UpdateAchievementResult>() {
@Override
public void onResult(UpdateAchievementResult result) {
if (result.getStatus().getStatusCode() == GamesStatusCodes.STATUS_ACHIEVEMENT_UNLOCKED) {
// play your sound. achievement was unlocked
}
}
});
This will increment your achievement by the amount of steps you want, and when it is done, it will return some of these codes. If the code is GamesStatusCodes.STATUS_ACHIEVEMENT_UNLOCKED
, then it means that increment unlocked the achievement and you can play your sound.
来源:https://stackoverflow.com/questions/22369199/android-play-a-sound-on-achievement-unlocked