【问题标题】:Android Google Fitness API for stepsAndroid Google Fitness API 的步骤
【发布时间】:2016-06-27 02:30:39
【问题描述】:

我的 Android 应用中有一项服务,用于计步。我健身和历史 API 用于获取每日和每周的步数。一切正常,直到突然步数始终为 0!该应用似乎使用上述代码连接到健身 api

private void buildFitnessClient() {
    mGoogleApiFitnessClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.SENSORS_API)
            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.RECORDING_API)
            .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
            .addConnectionCallbacks(
                    new GoogleApiClient.ConnectionCallbacks() {
                        @Override
                        public void onConnected(Bundle bundle) {
                            Log.i(TAG, "Google Fit connected.");
                            mTryingToConnect = false;
                            Log.d(TAG, "Notifying the UI that we're connected.");
                            notifyUiFitConnected();

                        }

                        @Override
                        public void onConnectionSuspended(int i) {
                            mTryingToConnect = false;
                            if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                                Log.i(TAG, "Google Fit Connection lost.  Cause: Network Lost.");
                            } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                                Log.i(TAG, "Google Fit Connection lost.  Reason: Service Disconnected");
                            }
                        }
                    }
            )
            .addOnConnectionFailedListener(
                    new GoogleApiClient.OnConnectionFailedListener() {
                        // Called whenever the API client fails to connect.
                        @Override
                        public void onConnectionFailed(ConnectionResult result) {
                            mTryingToConnect = false;
                            notifyUiFailedConnection(result);
                        }
                    }
            )
            .build();
}

为了获取每日步数,我使用了这个函数

private void getStepsToday() {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();

    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    long endTime = cal.getTimeInMillis();

    cal.setTime(now);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    long startTime = cal.getTimeInMillis();

    DebugLogger.debug("Value of startTime = "+startTime+" - endTime = "+endTime);
    DebugLogger.debug("Test for getting steps "+getStepsCount(startTime,endTime));

    final DataReadRequest readRequest = new DataReadRequest.Builder()
            .read(DataType.TYPE_STEP_COUNT_DELTA)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

    DataReadResult dataReadResult = Fitness.HistoryApi.readData(mGoogleApiFitnessClient, readRequest).await(1, TimeUnit.MINUTES);
    DataSet stepData = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);


    int totalSteps = 0;
    DebugLogger.debug("GetStepsToday - Size in for loop "+stepData.getDataPoints().size());
    for (DataPoint dp : stepData.getDataPoints()) {
        for(Field field : dp.getDataType().getFields()) {
            int steps = dp.getValue(field).asInt();
            DebugLogger.debug("GoogleFit Service - debugging - steps count "+steps);
            totalSteps += steps;
        }
    }
    publishTodaysStepData(totalSteps, getGoalStep(goalEnabledDate));
}

正如我提到的,一切正常,但突然 stepData.getDataPoints() 大小始终为 0,返回 0 步。我为我的调试和发布密钥库创建了新的 Oath 密钥,我什至更改了我的应用程序包名称并在 Android API 控制台中重新创建了一个项目,但什么也没有。有什么我应该改变权限或什么的吗?我需要在我的应用程序的任何地方声明我的密钥吗?

【问题讨论】:

    标签: android google-api google-fit google-fit-sdk


    【解决方案1】:

    检查您设备上的 Google Play 服务版本,并确保您的开发主机上有最新的 Google Play 服务客户端库。

    apply plugin: 'com.android.application'
    ...
    
    dependencies {
    compile 'com.google.android.gms:play-services-fitness:9.0.1'
    }
    
    • 打开 Android SDK 管理器。
    • 在 Extras 下,找到 Google Play services 和 Google Repository。
    • 如果这些包的状态不是“已安装”,请同时选择它们并单击“安装包”。

    此外,如果设置了 APIsAuth,请检查 Google Developers Console。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多