如何将步骤值(dataPoint.getValue(field))重置为ondataPoint func google fit

时间:2016-10-27 11:10:13

标签: java android api google-api-client google-fit

我试图从用户算起一步。我使用google fit api,但有一些我没有得到的东西。首先,这是我的代码:

    private void setUpGoogleApiClient() {
    apiClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.SENSORS_API)
            .addApi(ActivityRecognition.API)
            .addApi(Fitness.HISTORY_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

 @Override
public void onDataPoint(DataPoint dataPoint) {
    for (final Field field : dataPoint.getDataType().getFields()) {
        final Value value = dataPoint.getValue(field);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stepCount.setText(value.toString());
                if (DetectManager.getInstance(MainActivity.this).getMovementAsJson() != null)
                    fieldName.setText(DetectManager.getInstance(MainActivity.this).getMovementAsJson().getMovement());
                totalKmsDone = (float) value.asInt();
                Log.e("Steps: ", "Value " + value.toString());

                totalCaloriesBurned.setText(calories.calculateCalories((int) totalKmsDone, MainActivity.this));
                distance.setText(calories.calculateDistance((int) totalKmsDone, MainActivity.this));
                historyManager.animateTheView((int) (totalKmsDone / 10000 * 100), value.asInt(), Constant.AT_THE_MOMENT_STEP_COLOR, goal_indicator_view, null);
            }
        });
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Intent intent = new Intent(this, ActivityRecognizedService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, 3000, pendingIntent);

    DataSourcesRequest dataSourceRequest = new DataSourcesRequest.Builder()
            .setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
            .setDataSourceTypes(DataSource.TYPE_DERIVED)
            .build();

    ResultCallback<DataSourcesResult> dataSourcesResultCallback = new ResultCallback<DataSourcesResult>() {
        @Override
        public void onResult(DataSourcesResult dataSourcesResult) {
            for (DataSource dataSource : dataSourcesResult.getDataSources()) {
                if (DataType.TYPE_STEP_COUNT_CUMULATIVE.equals(dataSource.getDataType())) {
                    registerFitnessDataListener(dataSource, DataType.TYPE_STEP_COUNT_CUMULATIVE);
                }
            }
        }
    };
    Fitness.SensorsApi.findDataSources(apiClient, dataSourceRequest).setResultCallback(dataSourcesResultCallback);
}

    private void registerFitnessDataListener(DataSource dataSource, DataType dataType) {

    SensorRequest request = new SensorRequest.Builder()
            .setDataSource(dataSource)
            .setDataType(dataType)
            .setSamplingRate(3, TimeUnit.SECONDS)
            .build();

    Fitness.SensorsApi.add(apiClient, request, this)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {

                    }
                }
            });
}

我的问题是: 1.如何每天重置值(dataPoint.getValue(field);)。我想只计算用户每天的步数。

  1. 我将 DataType.TYPE_STEP_COUNT_CUMULATIVE 更改为 DataType.TYPE_STEP_COUNT_DELTA ,但它没有用。我一次又一次地测试,但它没有使用DataType.TYPE_STEP_COUNT_DELTA。我认为这是问题任何人都可以告诉我如何调用 DataType.TYPE_STEP_COUNT_DELTA
  2. 谢谢你的帮助。

0 个答案:

没有答案