1

TL;博士; 如果使用 Google+ 登录,GoogleFit Api 客户端无法连接

所以...我在同时使用 GoogleFit 和 Google+ api 时遇到了问题。我正在使用 Google+ 登录用户并使用 GoogleFit 来检索健身。

除了 Google+,我还有其他几个登录选项,例如 Facebook 和 Twitter。我的问题是,如果用户使用 Google+ 登录,则用户将无法再连接到 Google Fit 客户端。基本上,当按下连接到 GoogleFit 的按钮时,什么也没有发生。如果用户使用 Facebook 或 Twitter 进行身份验证,GoogleFit 客户端可以正常连接......

以下是此活动的一些相关代码:

Google+ 客户端:

 private GoogleApiClient buildGoogleApiClient() {
    return new GoogleApiClient.Builder(this)    
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();
}

Google Fit 客户端,只要用户按下按钮将 GoogleFit 链接到应用程序,就会调用此方法:

public void buildFitnessClient(Button b) {
    // Create the Google API Client
    fitConnectButton = b;
    mClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mClient.connect();
}

生命周期的东西:

 @Override
public void onConnected(Bundle bundle) {
    mSignInClicked = false;

    if(mGoogleServices != null) {
        Plus.PeopleApi.loadVisible(mGoogleServices, null).setResultCallback(this);
        userData = getProfileInformation();
    }
    if (hasWearDevice) mClient.connect();
}
 @Override
protected void onStart() {
    super.onStart();
    // Connect to G+ api
    if(mGoogleServices != null) mGoogleServices.connect();
    // Connect to the Fitness API
    if (hasWearDevice) mClient.connect();
}

@Override
public void onStop() {
    super.onStop();
    if(mGoogleServices != null) {
        if(mGoogleServices.isConnected()) mGoogleServices.disconnect();
    }
    if(hasWearDevice) {
        if(mClient.isConnected()) mClient.disconnect();
    }
}

有什么建议么?

4

2 回答 2

1

这不是一个解决方案,而是一个建议(我无法评论)。

可能问题出在您只能与 GoogleApiClient 连接一次。

您已与 Google+ 范围连接,因此当您尝试与 Fit 范围连接时,它无法工作,因为您已经连接。

也许您可以使用两种类型的连接:

  • 仅适用于 Google Plus 的一个
  • 一款带有 Google Plus 和 Google Fit 范围的产品。

它可能是这样的:

mClient = new GoogleApiClient.Builder(this)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .addScope(Plus.SCOPE_PLUS_PROFILE)
                .addApi(Fitness.API)
                .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

希望它可以帮助....

于 2015-03-16T09:51:59.880 回答
1

我最终通过为每个客户端使用不同的回调和 connectionFailed 侦听器来解决我的问题。

我的 GoogleFitClient 构建器最终看起来像这样:

public void startFitnessClient() {
    mGoogleFitClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    if (hasWearDevice) mGoogleFitClient.connect();
                }

                @Override
                public void onConnectionSuspended(int i) {
                    if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                        Log.i(LOG_TAG, "Connection lost.  Cause: Network Lost.");
                    } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                        Log.i(LOG_TAG, "Connection lost.  Reason: Service Disconnected");
                    }
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    // The failure has a resolution. Resolve it.
                    // Called typically when the app is not yet authorized, and an
                    // authorization dialog is displayed to the user.
                    if (!authInProgress) {
                        try {
                            Log.i(LOG_TAG, "Attempting to resolve failed connection");
                            authInProgress = true;
                            connectionResult.startResolutionForResult(BaseActivity.this, REQUEST_OAUTH);
                        } catch (IntentSender.SendIntentException e) {
                            Log.e(LOG_TAG, "Exception while starting resolution activity", e);
                            Crashlytics.logException(e);
                        }
                    }
                }
            })
            .build();
}

这是我的 Google+ 客户端。

private void buildGoogleApiClient() {
    mGooglePlusClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    mSignInClicked = false;

                    if(mGooglePlusClient != null) {
                        Plus.PeopleApi.loadVisible(mGooglePlusClient, null).setResultCallback(BaseActivity.this);
                        userData = getProfileInformation();
                    }
                }

                @Override
                public void onConnectionSuspended(int i) {
                    mGooglePlusClient.connect();
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    mConnectionResult = connectionResult;
                    if (!connectionResult.hasResolution()) {
                        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), BaseActivity.this, 0).show();
                        return;
                    }

                    if (!mIntentInProgress) {
                        if (mSignInClicked) {
                            resolveSignInError();
                        }
                    }
                }
            })
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();
}

对于我在调试过程中记录每个步骤所观察到的情况是,身份验证意图调用发生在调用内部onConnectionFailedstartResolutionForResult并且当连接 Google+ 客户端后它们共享相同的回调侦听器时,GoogleFit 客户端从未进行过回调。通过将它们分开,可以保证它们现在被调用。

于 2015-03-16T15:31:01.583 回答