整合Google plus时出现奇怪的错误

时间:2014-06-13 07:47:34

标签: android google-plus

我正在通过此网址https://developers.google.com/+/mobile/android/sign-in在我的应用程序中集成google plus但我无法选择google plus帐户登录,而在ConnectionResult中我总是得到ConnectionResult {statusCode = SIGN_IN_REQUIRED,resolution = PendingIntent {4223c668 :android.os.BinderProxy@4224b9c0}} ... 我尝试了很多链接,并创建并删除了客户端ID,它不适合我 这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
                    .addApi(Plus.API)
        .addScope(Plus.SCOPE_PLUS_LOGIN).build();

    btnGooglePlus = (SignInButton) findViewById(R.id.sign_in_button);
    btnGooglePlus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.sign_in_button
                    && !mGoogleApiClient.isConnecting()) {
                mSignInClicked = true;
            }
        }
    });


}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        if (resultCode != RESULT_OK) {
            mSignInClicked = false;
        }

        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }
}

@Override
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();
        }
    }
}

@Override
public void onConnected(Bundle arg0) {
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionSuspended(int cause) {
    mGoogleApiClient.connect();
}

/* A helper method to resolve the current ConnectionResult error. */
private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {


    mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(this,       RC_SIGN_IN);

        } catch (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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你不能在OnClickListener中做任何事情。

您需要从resolveSignInError()致电onClickSIGN_IN_REQUIRED会识别{{1}}并相应地启动登录流程。