This guide shows you how to use the achievements APIs in an Android application
to unlock and display achievements in your game. The APIs can be found
in the com.google.android.gms.games.achievement
package.
Before you begin
If you haven't already done so, you might find it helpful to review the achievements game concepts.
Before you start to code using the achievements API:
- Follow the instructions for installing and setting up your app to use Google Play games services in the Set Up Google Play Services SDK guide.
- Define the achievements that you want your game to unlock or display, by following the instructions in the Google Play Developer Console guide.
- Download and review the achievements code samples in the Android samples page.
- Familiarize yourself with the recommendations described in Quality Checklist.
Once the player is signed in and the GoogleApiClient is
connected, your game can start using the achievements APIs.
Unlocking achievements
To unlock an achievement, call the unlock() method and and pass in the
achievement ID.
Games.Achievements.unlock(mGoogleApiClient, "my_achievement_id");
If the achievement is of the incremental type (that is, several steps are required to
unlock it), call increment() instead.
Games.Achievements.increment(mGoogleApiClient, "my_incremental_achievment_id", 1);
You do not need to write additional code to unlock the achievement; Google Play games services automatically unlocks the achievement once it reaches its required number of steps.
A good practice is to define the achievement IDs in the strings.xml file, so
your game can reference the achievements by resource ID. When making calls to update and load achievements, make sure to also follow these best practices to avoid exceeding your API quota.
Displaying achievements
To show a player's achievements, call getAchievementsIntent() to get an Intent to create the default achievements UI. Your game can then bring up the UI by calling startActivityForResult. In the following snippet, REQUEST_ACHIEVEMENTS is an arbitrary integer used as the request code.
startActivityForResult(Games.Achievements.getAchievementsIntent(mGoogleApiClient),
REQUEST_ACHIEVEMENTS);
An example of the default achievements UI is shown below.