1

我的 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 控制台中重新创建了一个项目,但什么也没有。有什么我应该改变权限或什么的吗?我需要在我的应用程序的任何地方声明我的密钥吗?

4

1 回答 1

0

检查您设备上的 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。
  • 如果这些包的状态与已安装不同,请同时选择它们并单击安装包。

此外,如果设置了APIAuth,请在 Google Developers Console 中检查。

于 2016-06-26T18:30:39.030 回答