Google Play游戏服务获得成就列表

时间:2014-08-07 10:13:15

标签: android google-api google-play-games achievements

我在游戏中集成了Google Play游戏服务。现在我想要在不启动achievements intent的情况下检索成就列表。

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

感谢您的帮助;)

1 个答案:

答案 0 :(得分:1)

可以在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中运行,因为在等待加载成就时可能需要一些时间才能完成。

相关问题