【问题标题】:updating calories TYPE_CALORIES_EXPENDED into Google Fit将卡路里 TYPE_CALORIES_EXPENDED 更新到 Google 健身中
【发布时间】:2021-12-16 18:48:56
【问题描述】:

我想在 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;
        }
    }

我获得了成功,但卡路里计数未反映。

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

【问题讨论】:

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


    【解决方案1】:

    根据此documentation,步数/距离/活动时间/卡路里的值与 Fit 应用程序的值不匹配,因为 Google Fit 更新是如何发布的,Google Fit 应用程序可能有更新的版本我们的数据分析代码比 Google Play 服务发布的频率低。

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

    我们正在努力改善这种情况,让应用可以请求我们立即处理云同步以使所有内容保持同步,并使其在输入重要数据时自动进行同步或变化。

    这也可能是由于未读取正确的数据源造成的。要匹配 Google Fit 的值,您应该读取如下数据:

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

    【讨论】:

    • 感谢您的回复。我说的是更新数据(未读取),因此需要 DataUpdateRequest 并且我能够成功更新步数,但如果是卡路里,我无法更新 api 返回成功,但卡路里计数仍然相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多