我正在将Google Play游戏服务集成到我的游戏中。现在,我想检索成就列表而不启动achievements intent

我想要此意图后面的列表,以便可以使用此信息填充自己的UI元素。
我没有使用旧的GooglePlayServicesClient,而是使用了GoogleApiClient!

谢谢你的帮助 ;)

最佳答案

检索成就列表的代码可以在this answer中找到。

以下是代码段-有关完整说明,请参见链接的答案:

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();
}


该代码应在AsyncTask中运行,因为在等待成就加载时可能需要一些时间才能完成。

07-24 09:47
查看更多