【问题标题】:Waiting for Play Games to connect before displaying leaderboard在显示排行榜之前等待 Play 游戏连接
【发布时间】:2014-07-05 23:42:17
【问题描述】:

我不喜欢 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 然后尝试显示排行榜。

任何帮助将不胜感激

【问题讨论】:

    标签: android google-play-services leaderboard


    【解决方案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 设置为适当的值 - 并确保在登录完成后将其清除。

    【讨论】:

    • 嗨@free3dom 非常感谢,从 onSignInSucceeded() 中启动排行榜的唯一问题是,这似乎不仅在用户登录时被调用,而且在应用程序启动时也被调用/重新启动。这意味着如果用户在未注销的情况下退出应用程序然后重新启动它,则会自动显示排行榜。这是我绝对不想要的。你知道我可以如何减轻这种行为吗?再次感谢!
    • 确实,这就是我在最后的注释中的意思。理想情况下,当您(例如)想要在成功登录时显示排行榜时,您应该使用某种全局状态(可能是成员变量)并将其设置为某些已定义的代码。如果不清楚,请告诉我,我将添加一些代码来说明我的想法:)
    • 嗨@free3dom - 我确实尝试使用布尔值并检查 onSignInSucceeded() 中的布尔值状态,但它变得非常混乱和混乱!如果您能用一些代码解释一下,我将不胜感激 非常感谢 :-) 干杯!
    • 添加了一个例子。希望对您有所帮助,如果有什么不清楚的地方,请询问。
    • 没问题。编码愉快!
    猜你喜欢
    • 1970-01-01
    • 2013-05-12
    • 2017-09-22
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 2013-05-12
    • 2016-08-29
    • 1970-01-01
    相关资源
    最近更新 更多