Android谷歌API登录
我在登录GoogleAPI时遇到了很多问题。 我见过许多可能的实现,但似乎没有一个真正可行 以下是#1的来源: 这里的问题是,当我在应用程序启动后注销并再次登录时,UI不会更改/onConnected不会被调用,但当我切换活动以便它尝试再次连接时,它会立即调用onConnected。当我将super.onActivityResult(requestCode、resultCode、data)放入Android谷歌API登录,android,login,google-api,onactivityresult,Android,Login,Google Api,Onactivityresult,我在登录GoogleAPI时遇到了很多问题。 我见过许多可能的实现,但似乎没有一个真正可行 以下是#1的来源: 这里的问题是,当我在应用程序启动后注销并再次登录时,UI不会更改/onConnected不会被调用,但当我切换活动以便它尝试再次连接时,它会立即调用onConnected。当我将super.onActivityResult(requestCode、resultCode、data)放入super.onActivityResult时,它就工作了进入ActivityResult的顶部,但随后
super.onActivityResult时,它就工作了
进入ActivityResult的顶部,但随后连接成功,连接成功后,他似乎再次尝试连接,但就在我选择帐户之前,窗口消失了。
编辑:当出现错误时,它也不会显示任何错误对话框
第2个方法完全相同,但在ActivityResult和onConnectionFailed方法上有不同的
使用这种方法,我在选择一个帐户后收到一条错误消息,但之后还是登录了
有人帮我吗?好的,我终于找到了错误,当然,我最终会在那里搜索到
在创建Builder
时,我使用了enableautomanager()
,因此它显然复制了我想要执行的操作。用一种类似于#2的方法来解决问题
@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();
}
}
@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;
}
}
}