Android Google API登录

问题描述:

我在登录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号是完全一样的,但不同的onActivityResultonConnectionFailed方法,得到了它从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; 
     } 
    } 
} 

用这种方法我选择一个帐户后收到错误消息,但在获得登录之后仍然。

有人帮我吗?

好吧,我终于找到了错误,当然最后我会在那里搜索。

它是在创建Builder,我用enableAutoManage(),所以它显然重复我想要做的行动。切掉它并运行,使用类似于#2的方法。