0
我在登錄Google API時遇到很多問題。 我見過很多可能的實現,並且其中沒有一個真的可以工作。Android Google API登錄
這裏是第1張here:
@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();
}
}
這裏的問題是,當我退出並重新應用程序啓動後,用戶界面不會改變/ onConnected不叫,但是當我切換所以它嘗試重新連接,它立即調用onConnected。當我把super.onActivityResult(requestCode, resultCode, data);
放到onActivityResult
的頂部時,它就起作用了,但是當它連接成功並且連接成功後,他似乎再次嘗試連接,但是在我選擇一個帳戶之前窗口消失了。編輯:當出現問題時,它也不顯示任何錯誤對話框。
2號是完全一樣的,但不同的onActivityResult
和onConnectionFailed
方法,得到了它從here:
@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;
}
}
}
用這種方法我選擇一個帳戶後收到錯誤消息,但在獲得登錄之後仍然。
有人幫我嗎?