Google Play Game Services get achievements list

好久不见. 提交于 2019-12-04 15:49:20
free3dom

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!