Android - 在解锁成就上播放声音

问题描述:

我有一款Android游戏,当玩家解锁成就
时,我想播放声音,但我似乎无法找到一种方式来了解用户何时解锁成就,因为大部分我的成就是渐进式的我没有办法在客户端编写代码,唯一的方法就是在Google API显示成就框时调用一些回调函数,但在文档中找不到任何回调函数。我发现是这样的:集成Google Play服务成果和android通知

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)
{
    //
}

但我不知道如何实现该方法或如何调用该方法以及我没有在Google上运气

but I have no idea how to implement that or how to call that method and I had no luck on Google

使用以下代码:

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
                }
            }

        });

这会以您想要的步数增加您的成就,当它完成时,它会返回部分代码。如果代码是 GamesStatusCodes.STATUS_ACHIEVEMENT_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.