Android Play 服务登录 : Google Plus cancel button is not working properly

标签 android google-plus google-play-services

我正在尝试在我的 Android 应用程序中实现 Google+ 身份验证。为了做到这一点,我关注了this Google tutorial。 .

出现权限对话框时,如果用户点击登录,一切正常。但是,如果他单击取消,对话框将关闭几秒钟,然后重新显示。这将永远持续下去,因此无法正确取消操作。为什么会这样?

这是相关代码,改编自教程:

/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 0;

/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;
/* Track whether the sign-in button has been clicked so that we know to resolve
* all issues preventing sign-in without waiting.
*/
private boolean mSignInClicked;

/* Store the connection result from onConnectionFailed callbacks so that we can
 * resolve them when the user clicks sign-in.
 */
private ConnectionResult mConnectionResult;

/* A flag indicating that a PendingIntent is in progress and prevents
 * us from starting further intents.
 */
private boolean mIntentInProgress;

protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

/* A helper method to resolve the current ConnectionResult error. */
private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            startIntentSenderForResult(mConnectionResult.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;
            mGoogleApiClient.connect();
        }
    }
}

public void onConnectionFailed(ConnectionResult result) {
    if (!mIntentInProgress) {
        // Store the ConnectionResult so that we can use it later when the user clicks
        // 'sign-in'.
        mConnectionResult = result;

        if (mSignInClicked) {
            // The user has already clicked 'sign-in' so we attempt to resolve all
            // errors until the user is signed in, or they cancel.
            resolveSignInError();
        }
    }
}

// Login by email button click listener.
public class ButtonLoginGPlusClicked implements View.OnClickListener {
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.sign_in_button
                && !mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }
    }
}

@Override
public void onConnected(Bundle connectionHint) {
    // Save credentials.
    Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

    SharedPreferencesHelper.updateStringValue(
            LoginAsVerifiedTracker.this,
            R.string.preferences_user_id,
            currentPerson.getId());
    SharedPreferencesHelper.updateStringValue(
            LoginAsVerifiedTracker.this,
            R.string.preferences_user_name,
            currentPerson.getDisplayName());

    // Close.
    setResult(Activity.RESULT_OK);
    finish();
    return;
}

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

编辑:

这里是 onActivityResult 类:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case RC_SIGN_IN: {
            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }

            break;
        }
        case 1: {
            overridePendingTransition(R.anim.slide_right_to_left_enter,
                    R.anim.slide_right_to_left_exit);
            break;
        }
    }
}

这是我正在谈论的对话框:

enter image description here

最佳答案

根据 step 5 of the Google+ Sign In guide :

You should then reset the state of the flags when control returns to your Activity in onActivityResult.

protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if (requestCode == RC_SIGN_IN) {
      if (responseCode != RESULT_OK) {
        mSignInClicked = false;
      }
      mIntentInProgress = false;
      if (!mGoogleApiClient.isConnecting()) {
        mGoogleApiClient.connect();
      }
    }
  }

请注意针对 responseCode 的检查 - 只有当用户点击登录按钮时,responseCode 才等于 RESULT_OK。这确保取消按钮停止 onConnectionFailed() 中的 resolveSignInError() 调用(这是导致它永远循环的原因)。

关于Android Play 服务登录 : Google Plus cancel button is not working properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26493189/

相关文章:

java - 如何在android java中使用fasterxml jackson动态解析本地Json以获取固定的pojo类

ios - 无法插入时刻-Google Plus API

java - PlusClient.OnPersonLoadedListener 无法解析为类型

java - 使用纹理时 libgdx 不加载文件

c# - 使用 ASP.NET 生成可以在 Android 上下载的 vCard

android - 使用 LinearLayout 将按钮底部对齐

android - 添加 play-services-maps 依赖自动添加 glEsVersion 2.0 要求

python - Google+ 登录 - 服务器端流程 - Python - Google App Engine

javascript - Google+ 样式自动完成 + 下拉 jQuery 插件

android - 如何在 Android 应用程序中获取 Google Play 引荐来源网址