0

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

Community
  • 1
  • 1
DarkW
  • 575
  • 1
  • 6
  • 18

1 Answers1

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.

Ogen
  • 6,499
  • 7
  • 58
  • 124
  • Thank you, that worked for the incremental achievements! – DarkW Mar 14 '14 at 00:39
  • @DarkW You specifically stated incremental acheivements, are there other achievements that aren't working for you? – Ogen Mar 14 '14 at 22:57
  • yes, but I managed to workaround it, since i know when to unlock it. Thank you! – DarkW Mar 15 '14 at 00:31
  • @Ogen For some reason, that never worked for me. It always returns either status code `STATUS_OK` or `STATUS_NETWORK_ERROR_OPERATION_DEFERRED` but never `STATUS_ACHIEVEMENT_UNLOCKED` although I am sure that it is unlocked since a notification is popped up showing that. Any idea why that happens? – Hussein El Feky Aug 02 '16 at 21:54
  • @HusseinElFeky Hmm, maybe double check that your achievement is actually an incremental one. Other than that, maybe STATUS_OK is the same as STATUS_ACHIEVEMENT_UNLOCKED. The google achievement api documentation says STATUS_OK means that the operation was successful. Maybe they're interchangeable. – Ogen Aug 03 '16 at 08:35
  • @Ogen I have both normal achievements and incremental achievements in my game, and I call `unlockImmediate()` and `incrementImmediate()` for each type of them. However, the problem is that `STATUS_OK` is always called whether the operation was successful or the achievement was unlocked, so I can't differentiate between these two results. I can know when the normal achievements are unlocked, so that wasn't a problem for me (I dealt with it), but the problem now is in the incremental achievements as I explained earlier. – Hussein El Feky Aug 03 '16 at 12:55
  • @HusseinElFeky Hmmm, that's an odd situation. I recommend creating a question and adding your code. It will be a lot easier for people to help you that way. You can link me the question after you make it too. – Ogen Aug 04 '16 at 08:23
  • Now that `Games.Achievements` is deprecated we have to use https://developers.google.com/android/reference/com/google/android/gms/games/AchievementsClient#incrementImmediate(java.lang.String,%20int) however for me the result of the task is always false, no matter the task is completed or not. Though, the documentation states "This form of the API will attempt to update the user's achievement on the server immediately. The Boolean in a successful response indicates whether the achievement is now unlocked." Any idea? – Martin Braun Oct 12 '18 at 11:58