Google Play Game Services get achievements list

半世苍凉 提交于 2020-01-23 02:45:49

问题


I'm integrating Google Play Game Services in my game. Now I want to retrieve a list of achievements without starting the achievements intent.

I want the list behind that intent so that I can populate my own UI elements with this information. I'm not using the old GooglePlayServicesClient, I'm using the GoogleApiClient!

Thanks for your help ;)


回答1:


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.



来源:https://stackoverflow.com/questions/25179827/google-play-game-services-get-achievements-list

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