我在登录Google时遇到了很多问题。我已经看到了许多可能的实现,但其中没有一个看起来真正有效。
这里是来自这里的#1
@Override
public void onConnected(Bundle bundle) {
Log.d("MA", "onConnected");
mSignInOut.setVisibility(View.VISIBLE);
mSignInOut.setText(getResources().getText(R.string.signOut));
mSignInOut.setTextColor(getResources().getColor(R.color.signOutButton));
mLoggedInMessage.setText(getResources().getText(R.string.loggedInMessage) + " " + getUsername() + "!");
}
protected boolean isSignedIn() {
return (mGoogleApiClient != null && mGoogleApiClient.isConnected());
}
public void signInOut() {
if(isSignedIn()) {
Log.d("MA", "signinout: signing out");
mSignInClicked = false;
Games.signOut(mGoogleApiClient);
mGoogleApiClient.disconnect();
onLoggedOut();
} else {
Log.d("MA", "signinout: signing in");
mSignInClicked = true;
mGoogleApiClient.connect();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RQC_SIGN_IN) {
mResolvingError = false;
if (resultCode == RESULT_OK) {
// Make sure the app is not already connected or attempting to connect
if (!mGoogleApiClient.isConnecting() &&
!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (mResolvingError) {
// Already attempting to resolve an error.
return;
} else if (result.hasResolution()) {
try {
mResolvingError = true;
result.startResolutionForResult(this, RQC_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
showErrorDialog(result.getErrorCode());
mResolvingError = true;
}
}
// The rest of this code is all about building the error dialog
/* Creates a dialog for an error message */
private void showErrorDialog(int errorCode) {
// Create a fragment for the error dialog
ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
// Pass the error that should be displayed
Bundle args = new Bundle();
args.putInt("DIALOG_ERROR", errorCode);
dialogFragment.setArguments(args);
dialogFragment.show(getSupportFragmentManager(), "errordialog");
}
/* Called from ErrorDialogFragment when the dialog is dismissed. */
public void onDialogDismissed() {
mResolvingError = false;
}
/* A fragment to display an error dialog */
public static class ErrorDialogFragment extends DialogFragment {
public ErrorDialogFragment() { }
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the error code and retrieve the appropriate dialog
int errorCode = this.getArguments().getInt("DIALOG_ERROR");
return GooglePlayServicesUtil.getErrorDialog(errorCode,
this.getActivity(), RQC_SIGN_IN);
}
@Override
public void onDismiss(DialogInterface dialog) {
((MainActivity)getActivity()).onDialogDismissed();
}
}这里的问题是,当我在应用程序启动后再次登录时,UI不会改变/ onConnected不会被调用,但是当我切换活动以便它再次尝试连接时,它会立即调用onConnected。当我将super.onActivityResult(requestCode, resultCode, data);放到onActivityResult的顶部时,它起了作用,但之后它连接起来了,在成功连接之后,他似乎再次尝试连接,但就在我选择帐户之前,窗口就消失了。编辑:而且,当出了问题时,它也不会显示任何错误对话框。
数字2是完全相同的,但是对于不同的onActivityResult和onConnectionFailed方法,是从这里获得的。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RQC_SIGN_IN) {
mSignInClicked = false;
mResolvingError = false;
Log.d("MA", "resultCode: " + resultCode);
if (resultCode == RESULT_OK) {
mGoogleApiClient.connect();
} else {
Log.d("MA", "onActivityResult, resultCode != -1");
BaseGameUtils.showActivityResultError(this, requestCode, resultCode, R.string.signin_other_error);
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// If the sign in button was clicked or if auto sign-in is enabled,
// launch the sign-in flow
Log.d("BFA", "onConnectionFailed, connectionresult: " + result.getErrorCode());
if (mResolvingError) {
Log.d("BFA", "Already attempting to resolve an error.");
// Already attempting to resolve an error.
return;
}
if (mSignInClicked || mAutoLogin) {
mResolvingError = true;
mSignInClicked = false;
mAutoLogin = false;
if (!BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient, result,
RQC_SIGN_IN, getString(R.string.signin_other_error))) {
Log.d("MA", "resolveConnectionFailure false");
mResolvingError = false;
}
}
}使用这种方法,我在选择帐户后会收到一条错误消息,但是在选择帐户后,我还是会登录。
谁来帮帮我?
发布于 2015-02-10 21:00:32
好的,我终于发现了这个错误,当然,我最终会在那里搜索。
在Builder的创建过程中,我使用了enableAutoManage(),所以它显然重复了我想要做的操作。停下来工作,使用一种类似于第二种方法。
https://stackoverflow.com/questions/28437692
复制相似问题