在显示排行榜之前等待Play游戏连接

时间:2014-05-17 16:23:57

标签: android google-play-services leaderboard

我并不热衷于Play游戏服务的标准行为,以便在首次启动应用时自动尝试连接,因此我已禁用此功能。在我的主菜单中,我有一个显示分数'按钮。当用户按下此按钮时我想要发生的是:

  • 如果用户已连接(已登录),请继续并显示排行榜

  • 如果未连接用户,则显示连接对话框。连接后,显示排行榜

  • 在主菜单上,我将有一个额外的按钮"退出"只有在用户连接/登录时才会显示。

当用户点击按钮时,我正在执行以下操作:

代码

if (buttonPressed()){

    //Display connection dialogue and initiate log in
    getGameHelper().beginUserInitiatedSignIn();


    //Check if the user is signed in before continuing
    if (getGameHelper.isSignedIn()){

        startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), myLeaderBoardID), 1);

    }

}

如果用户未连接:系统会向用户显示连接对话框 - 这样可以正常使用。然后他们就可以登录了。一旦他们完成了这个,就没有其他事情发生了(代码已经移动,因此没有显示排行榜,因为用户没有登录 - 如果我没有检查到看看用户是否在这里登录,应用程序就会崩溃)。如果用户再次按下该按钮,它将显示排行榜。

如何只需按一下按钮即可完成所有这些操作?

我想要的是,如果用户未登录,则显示登录对话框,然后在用户登录后立即显示排行榜。我需要让 startActivityForResult 等到用户完成登录后才能使用。

简而言之

我需要让我的代码等到它尝试显示排行榜之前连接到Play

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您可以按如下方式收到登录成功/失败的通知:

getGameHelper().setup( 
   new GameHelper.GameHelperListener() {  
      @Override
      public void onSignInSucceeded() {
         // execute code on successful sign-in
         // for example, here you could show your leaderboard
      }
      @Override
      public void onSignInFailed() {
         // execute code on failed sign-in
      }
   };
);

您当然应该在尝试登录之前执行之前。然后,您可以在登录成功时显示您的排行榜。此代码应放在您创建游戏助手的位置(即在执行buttonPressed()代码之前)。

完成此代码后,您应该将buttonPressed()代码更改为如下所示:

if ( buttonPressed() ) {

   // check if user already signed-in and show leaderboard; otherwise do sign-in
   if ( getGameHelper.isSignedIn() )  {
      startActivityForResult( Games.Leaderboards.getLeaderboardIntent( getApiClient(), myLeaderBoardID ), 1 );
   }
   else   {
      getGameHelper().beginUserInitiatedSignIn();
      // NOTE: do nothing further here; show the leaderboard in 
      //       the listener's onSignInSucceeded() 
   }
}

最后一点:您创建的监听器将被调用所有登录操作,因此如果您需要在多个位置使用此功能(例如,如果您想对成就执行相同操作),那么您将需要使用一些信号表明在成功登录时需要采取的措施,并在onSignInSucceeded()中采取正确的行动。

表示已登录成功的操作:

将此代码添加到您的类(全局范围)

public final static int NO_ACTION = 0;
public final static int SHOW_LEADERBOARD = 1;
public final static int SHOW_ACHIEVEMENTS = 2;

public int signInAction = NO_ACTION;

接下来在登录前设置操作(根据登录的位置):

if ( buttonPressed() ) {

   // check if user already signed-in and show leaderboard; otherwise do sign-in
   if ( getGameHelper.isSignedIn() )  {
      startActivityForResult( Games.Leaderboards.getLeaderboardIntent( getApiClient(), myLeaderBoardID ), 1 );
   }
   else   {

      // NEW: request leaderboard to be shown upon sign in
      signInAction = SHOW_LEADERBOARD;
      // NEW---------------------------------------------- 
      getGameHelper().beginUserInitiatedSignIn();
      // NOTE: do nothing further here; show the leaderboard in 
      //       the listener's onSignInSucceeded() 
   }
}

最后更改侦听器以响应设置的登录操作:

getGameHelper().setup( 
   new GameHelper.GameHelperListener() {  
      @Override
      public void onSignInSucceeded() {
         if ( signInAction == SHOW_LEADERBOARD )  {
            // show your leaderboard here
         }
         else if ( signInAction == SHOW_ACHIEVEMENTS )  {
            // show achievements here
         }

         // important! reset the sign-in action so that any subsequent sign-in
         // attempts do not re-use the currently set action!
         signInAction = NO_ACTION;
      }
      @Override
      public void onSignInFailed() {
         // execute code on failed sign-in

         // important! it should also be cleared in case of an error
         signInAction = NO_ACTION;
      }
   };
);

当然,这只是实现这一目标的一种方法,但它应该适用于大多数用途。请务必在执行登录前将signInAction设置为适当的值,并确保在登录完成后将其清除。