onSignInSucceeded的奇怪行为

时间:2014-09-25 23:37:11

标签: android google-play-services google-play-games

我从2天前遇到麻烦,我找不到任何解决方案。

我有两个按钮,一个用于登录,另一个用于注销。如果用户已登录,则显示“注销”按钮,如果用户未登录,则会显示登录按钮。

问题从这里开始;当用户点击登录按钮时,我隐藏登录按钮并显示登出按钮并将其文本设置为“正在连接...”如果登录流程成功我想将文本更改为“注销”。如果流程失败,我会隐藏注销并再次显示登录按钮。 问题是,即使登录流程成功,注销按钮的文本仍保持为“连接...”。

这是我的代码:(我的类没有扩展到BaseGameActivity)

mHelper = new GameHelper(this, GameHelper.CLIENT_ALL);
        GameHelper.GameHelperListener listener = new GameHelper.GameHelperListener() {
            @Override
            public void onSignInSucceeded() {
                signin.setVisibility(View.GONE);
                signout.setVisibility(View.VISIBLE);
                signout.setText(R.string.signout);
            }
            @Override
            public void onSignInFailed() {
                signout.setVisibility(View.GONE);
                signin.setVisibility(View.VISIBLE);
            }
        };mHelper.setup(listener);

和点击监听器:

 case R.id.sign_in_button:
      signout.setText(R.string.loading);
      signout.setVisibility(View.VISIBLE);
      mHelper.beginUserInitiatedSignIn();
      signin.setVisibility(View.GONE);
      break;
case R.id.sign_out_button:
      mHelper.signOut();
      signin.setVisibility(View.VISIBLE);
      signout.setVisibility(View.GONE);
      break;

更新:

登录和注销过程有效,因为我可以将分数提交到排行榜并显示它。

3 个答案:

答案 0 :(得分:1)

游戏样本(https://github.com/playgameservices/android-basic-samples)最近已更新,不会扩展BaseGameActivity。我建议您详细查看它们,但以下是管理登录所需的高级项目。

  1. 声明一个类成员GoogleApiClient mGoogleApiClient并在onCreate()方法中初始化它:

    mGoogleApiClient = new GoogleApiClient.Builder(this)    
                    .addConnectionCallbacks(this)  
                    .addOnConnectionFailedListener(this)  
                    .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)  
                    .addApi(Games.API).addScope(Games.SCOPE_GAMES)  
                    // add other APIs and scopes here as needed  
                    .build();
    
  2. 登录按钮的onClick()方法中的
  3. mSignInClicked = true;
    mGoogleApiClient.connect();
    
  4. 和签出按钮

    Games.signOut(mGoogleApiClient);   
    mGoogleApiClient.disconnect();
    showSignInBar(); // also hides the sign out bar
    
  5. 实现侦听器接口 GoogleApiClient.ConnectionCallbacks GoogleApiClient.OnConnectionFailedListener

  6. 实现侦听器方法:

    public void onConnected(Bundle connectionHint) {
        showSignOutBar();
        // initialize other data that needs a connection.
    }
    
    public void onConnectionSuspended(int cause) {
        // start the connection process again.
        mGoogleApiClient.connect();
    }
    
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (this.mResolvingConnectionFailure) {
            Log.d(TAG, "onConnectionFailed() ignoring connection failure; already resolving.");
            return;
        }
        // only try to resolve the connection issue if we actually want to connect
        if (mSignInClicked || mAutoStartSignInFlow) {
            mAutoStartSignInFlow = false;
            mSignInClicked = false;
            mResolvingConnectionFailure = BaseGameUtils
                .resolveConnectionFailure(this, mGoogleApiClient,
                        connectionResult, RC_SIGN_IN,
                        getString(R.string.signin_other_error));
        }
        showSignInBar();
    }
    
  7. 处理 onActivityResult()

    中的登录意图
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case RC_SIGN_IN:
                mSignInClicked = false;
                mResolvingConnectionFailure = false;
                if (resultCode == RESULT_OK) {
                    mGoogleApiClient.connect();
                } else {
                    BaseGameUtils.showActivityResultError(this, requestCode, resultCode,
                            R.string.signin_failure, R.string.signin_other_error);
                }
                break;
    
        //…..
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    

答案 1 :(得分:0)

我找到了这个答案Google Play Game Services: strange sign in behavior

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
 super.onActivityResult(requestCode, resultCode, data);
 ....
 <our code here>
}

这个答案告诉我们覆盖onActivityResult方法,由此BaseGameActivity处理回调。但我必须将我的课程扩展到BaseGameActivity并使用

beginUserInitiatedSignIn();

mHelper.beginUserInitiatedSignIn();

如果我没有在用户退出时将我的课程扩展到BaseGameActivity并且想要第二次登录注销按钮的文本不会改变,则仍然是“连接...”。

答案 2 :(得分:0)

如果从BaseGameActivity扩展您的课程会有问题吗?如果您使用该课程,可能您的问题(以及将来的其他问题)将会消失。

有关登录和退出以及所有Game API的所有必要方法都用于扩展该类。

我鼓励你使用它。

这里有Google的登录教程,希望对您有所帮助:

https://developers.google.com/games/services/training/signin?hl=es