Android Play服务登录:Google Plus取消按钮工作不正常

Android Play服务登录:Google Plus取消按钮工作不正常,android,google-plus,google-play-services,Android,Google Plus,Google Play Services,我正在尝试在我的Android应用程序中实现Google+身份验证。为了做到这一点,我遵循了 当出现权限对话框时,如果用户单击登录,一切正常。但是,如果他单击“取消”,对话框将关闭几秒钟,然后显示“返回”。这将永远持续下去,因此无法正确取消操作。为什么会这样 这是根据教程改编的相关代码: /* Request code used to invoke sign in user interactions. */ private static final int RC_SIGN_IN = 0; /*

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

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

这是根据教程改编的相关代码:

/* 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;
        }
    }
}
这就是我所说的对话:

根据:

然后,当控件返回到
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();
      }
    }
  }

请注意,仅当用户点击登录按钮时,才对
响应代码进行检查
响应代码
等于
结果_OK
。这可以确保取消按钮停止调用
onConnectionFailed()
(这是导致它永远循环的原因)。

那么你的
onActivityResult
方法在哪里?抱歉,忘了添加它。我已经编辑了这个问题。您好,您能为使用google plus登录一个活动和注销另一个活动提供一些帮助吗?根据重定向到导航,如何处理用户何时登录am saved loginstatus。