GoogleApiClient连接始终首次失败但第二次成功

Mav*_*ven 7 android google-api-client

我有一个谷歌加登录设置为应用程序通过GoogleApiClient.

每当应用程序第一次安装并尝试通过GoogleApiClient它进行连接时,永远不会成功并始终onConnectionFailedresult包含:

ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{4130e760: android.os.BinderProxy@4130e700}}
Run Code Online (Sandbox Code Playgroud)

但是当第二次登录时,它被称为成功并onConnected点击.为什么有可能在第一次尝试中取得成功?

我的Builder参数有问题吗?

public void connectGoogleApi() {
    mGoogleApiClient = new GoogleApiClient.Builder(mainAppContext).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN).build();
    mGoogleApiClient.connect();
}

public void onConnectionFailed(ConnectionResult result) {
    if (!result.hasResolution()) {
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
        return;
    }

    if (!mIntentInProgress) {
        // Store the ConnectionResult for later usage
        mConnectionResult = result;
        resolveSignInError();
    }

}
Run Code Online (Sandbox Code Playgroud)

Pio*_*ski 1

我遇到了同样的问题,再次调用“connect()”,这次在“onConnected”方法中修复了它。奇怪的。

@Override
   public void onConnected(final Bundle arg0) {
   Logger.log("On connected");
   DevicePreferences.getGoogleApiClient().connect();
}
Run Code Online (Sandbox Code Playgroud)