【问题标题】:GoogleApiClient will not connect when adding Fitness API添加 Fitness API 时 GoogleApiClient 将无法连接
【发布时间】:2016-07-09 03:55:59
【问题描述】:

我的服务中有一个 Fitness GoogleApiClient 可以完美运行...直到我添加了 Fitness API。

    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Wearable.API)
            .addApi(LocationServices.API)
            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.RECORDING_API)
            .useDefaultAccount()
            .build();

我的 WatchService(适用于 Android Wear)中具有完全相同的设置,它可以完美运行,完全没有任何问题......但是一旦我将它添加到手机应用程序中的服务......它就无法连接完全没有。

如果我取出这两条线 - 它会立即连接。

            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.RECORDING_API)

建议?

因此,Fitness.HISTORY_API 似乎需要在手机应用程序上登录 - 但不是在手表上......奇怪......我只尝试了 Fitness.HISTORY_API,但它仍然需要登录。

【问题讨论】:

  • 那么你的OnConnectionFailedListener 被叫到了吗?你处理连接错误吗? Google Fit API 确实需要登录(与 Wearable 和 LocationServices API 不同)。
  • 是的 - 我想你是对的......为什么这需要在手机上登录,但在 Wear 上不需要? onConnectionFailed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{510f69a: android.os.BinderProxy@8781bcb}, message=null}
  • 唯一不需要身份验证的 Google Fit API 是 HistoryApi.readDailyTotal() API - 你到底想做什么?
  • 这正是我想要做的......只需阅读每日总数。
  • 那你为什么有RECORDING_API

标签: android wear-os google-fit


【解决方案1】:

我也曾遇到过SIGN_IN_REQUIRED 错误,它甚至不会要求登录我的 Google 帐户,这听起来像是发生在你身上的事情。为了请求登录,我使用了ConnectionResult.startResolutionForResult,如下所示:

.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult result) {
        Log.i(TAG, "Google Play services connection failed. Cause: " + result.toString());
        Snackbar.make(/*...*/).show();
        try {
            Log.i(TAG, "Requesting login...");

            ////////////////////////// HERE //////////////////////////////
            result.startResolutionForResult(MainActivity.this, REQUEST_LOGIN);
        }
        catch (Exception e) {
            // Handle exception however you like
        }
    }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多