将卡路里TYPE_CALORIES_EXPENDED更新为Google健身

时间:2017-08-22 11:03:09

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

我想更新google fit api中的卡路里,我也尝试步数更新,并且https://developers.google.com/fit/android/history

给出的代码成功更新了步数

我编辑更新卡路里的代码但不幸的是我无法做到这一点

我使用以下代码:

    private DataUpdateRequest updateRequest() {
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        cal.add(Calendar.MINUTE, 0);
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.MINUTE, -500);
        long startTime = cal.getTimeInMillis();

// Create a data source
        DataSource dataSource = new DataSource.Builder()
                .setAppPackageName(this)
                .setDataType(DataType.TYPE_CALORIES_EXPENDED)
                .setStreamName(TAG + " - calories")
                .setType(DataSource.TYPE_RAW)
                .build();

// Create a data set
        DataSet dataSet = DataSet.create(dataSource);
// For each data point, specify a start time, end time, and the data value -- in this case,
// the number of new steps.
        DataPoint dataPoint = dataSet.createDataPoint()
                .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
        dataPoint.getValue(Field.FIELD_CALORIES).setFloat(0.0f);
        dataSet.add(dataPoint);

        Log.i(TAG, "Updating the dataset in the History API.");


        DataUpdateRequest request = new DataUpdateRequest.Builder()
                .setDataSet(dataSet)
                .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();
        return request;
    } 

       private class UpdateQuery extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... params) {
            DataUpdateRequest calorieDataSet=ResetCaloriesCount();


            com.google.android.gms.common.api.Status updateStatus =
                    Fitness.HistoryApi.updateData(mApiClient, calorieDataSet)
                            .await(1, TimeUnit.MINUTES);

// Before querying the data, check to see if the update succeeded.
            if (!updateStatus.isSuccess()) {
                Log.i(TAG, "There was a problem updating the dataset.");

            }

// At this point the data has been updated and can be read.
            Log.i(TAG, "Data update was successful.");
            return null;
        }
    }

我获得了成功,但没有反映卡路里数量。

注意:我的应用程序插入的所有数据都没有任何其他适合的应用

1 个答案:

答案 0 :(得分:0)

根据此documentation,由于Google健身适应更新的发布方式,步数/距离/有效时间/卡路里的值与Fit应用的值不匹配,Google健身应用可能会有更多我们的数据分析代码的最新版本比Google Play服务更新版本。

  

然而,我们的后端始终拥有最新版本的数据,并且最终会在那里处理所有内容,并且应该在几次云同步之后匹配。如果情况不是这样,请告诉我们。

     

我们正在努力让这种情况变得更好,因为应用可以请求我们立即处理云同步,以便所有内容都保持同步,并且还可以使其在输入重要数据时自动发生同步或变化。

这也可能是因为没有读取正确的数据源。要匹配Google健身的价值,您应该阅读以下数据:

DataReadRequest readRequest = new DataReadRequest.Builder()
       ...
       .aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
       ...
       .build();