我正在尝试在Google+
应用程序中实现Android
身份验证。为了做到这一点,我遵循了this Google tutorial。
当出现权限对话框时,如果用户点击登录,一切正常。但是,如果他点击 CANCEL ,对话框会关闭几秒钟,然后显示备份。这种情况永远存在,因此无法正确取消操作。为什么会这样?
这是相关的代码,改编自教程:
/* 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;
}
}
}
这是我正在谈论的对话:
答案 0 :(得分:6)
每step 5 of the Google+ Sign In guide:
当控件返回到
Activity
中的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
。这可以确保取消按钮停止resolveSignInError()
中的onConnectionFailed()
调用(导致它永远循环的原因)。