Google+登录按钮无法取消

时间:2015-07-02 16:13:25

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

我是Android开发的新手,希望能为我面临的问题提供一些建议。

我的应用要求我实施Google +登录按钮。

我的进步

  • 我遵循指南并完成了Google +登录按钮的所有必要设置步骤
  • 我可以使用登录按钮并检索个人资料电子邮件

我的问题

  • 点击Google +登录按钮后,选择"选择帐户"对话框显示,允许用户从多个可能的Gmail帐户中进行选择
  • 当用户单击某个帐户然后单击“确认”按钮时,一切正常
  • 但是,当用户点击对话框中的取消按钮时,对话框会消失并重新出现。即使单击后退按钮,对话框也会消失并重新出现。

这可以防止用户选择其他登录选项。

我想知道我的代码有什么问题,任何帮助都会受到赞赏。感谢。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // facebook sign in
    FacebookSdk.sdkInitialize(getApplicationContext());

    setContentView(R.layout.activity_sign_in);
    facebookLoginSetup(findViewById(android.R.id.content).getRootView());

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();

    SignInButton sign_in_button = (SignInButton) findViewById(R.id.sign_in_button);
    setGooglePlusButtonText(sign_in_button, getString(R.string.google_login_button_label));

    findViewById(R.id.sign_in_button).setOnClickListener(this);
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setMessage(getString(R.string.global_message_loading));
    mProgressDialog.setCancelable(false);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(mProgressDialog.isShowing()){
        mProgressDialog.dismiss();
    }

    // google
    if (requestCode == RC_SIGN_IN) {
        if (resultCode != RESULT_OK) {
            mSignInClicked = false;
        }

        mIntentInProgress = false;
        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.reconnect();
        }
    } else {
        // facebook
        // call registered call back method
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}


@Override
public void onConnectionSuspended(int cause)
{
    mGoogleApiClient.connect();
}


@Override
public void onClick(View v) {
    if (v.getId() == R.id.sign_in_button && !mGoogleApiClient.isConnecting()) {
        if(!mProgressDialog.isShowing()){
            mProgressDialog.show();
        }

        mSignInClicked = true;
        mGoogleApiClient.connect();
    }
}


@Override
public void onConnected(Bundle connectionHint) {
    mSignInClicked = false;
    if(mProgressDialog.isShowing()){
        mProgressDialog.dismiss();
    }

    if (Plus.AccountApi.getAccountName(mGoogleApiClient) != null) {
        String userEmail = Plus.AccountApi.getAccountName(mGoogleApiClient);
        createUser(userEmail);
    }
}


@Override
public void onConnectionFailed(ConnectionResult result) {

    if (!mIntentInProgress && result.hasResolution()) {
        try {
            Log.d(MainActivity.TAG, "onConnectionFailed keep retrying");
            mIntentInProgress = true;
            startIntentSenderForResult(result.getResolution().getIntentSender(),
                    RC_SIGN_IN, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            // The intent was canceled before it was sent.  Return to the default
            // state and attempt to connect to get an updated ConnectionResult.
            mIntentInProgress = false;
        }
    }
}


    // google custom button
protected void setGooglePlusButtonText(SignInButton signInButton, String buttonText) {
    for (int i = 0; i < signInButton.getChildCount(); i++) {
        View v = signInButton.getChildAt(i);

        if (v instanceof TextView) {
            TextView tv = (TextView) v;
            tv.setTextSize(15);
            tv.setTypeface(null, Typeface.NORMAL);
            tv.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
            tv.setText(buttonText);
            return;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

onActivityResult 方法中检查resultCode。

d_

现在 onConnectionFailed 不会执行 startIntentSenderForResult

答案 1 :(得分:0)

我也有同样的问题,用这个解决了

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString(PREF_ACCOUNT_NAME, "cancel");
                    editor.apply();
                    this.finish();

...谢谢

答案 2 :(得分:0)

我遇到了同样的问题。对于&#34;取消&#34; action,(responseCode!= RESULT_OK)也返回true,所以我在onActivityResult(..)方法中捕获它。这是代码:

 protected void onActivityResult(int requestCode, int responseCode, Intent data) {
    // TODO Auto-generated method stub
    if (requestCode == RC_SIGN_IN) {
        if (responseCode != RESULT_OK) {
                mSignInClicked = false;
        }else if (responseCode == RESULT_OK) {
            if (!mGoogleApiClient.isConnected()) {
                mGoogleApiClient.reconnect();                   
            }           
        }
        mIntentInProgress = false;
    } else {
        ParseFacebookUtils.onActivityResult(requestCode, responseCode,   data);
    }

}