android - 如何从 google fit 读取消耗卡路里数和剩余卡路里数

标签 android google-fit

如何从 google fit 读取消耗卡路里数和剩余卡路里数。到目前为止,我只能获取步数,而且我还需要卡路里数据。我还将体重和高度插入到history api中,如下所示。

 public  void saveUserHeight(int heightCentimiters) {
    // to post data
    float height = ((float) heightCentimiters) / 100.0f;
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.DAY_OF_YEAR, -1);
    long startTime = cal.getTimeInMillis();

    DataSet heightDataSet = createDataForRequest(
            DataType.TYPE_HEIGHT,    // for height, it would be DataType.TYPE_HEIGHT
            DataSource.TYPE_RAW,
            height,                  // weight in kgs
            startTime,              // start time
            endTime,                // end time
            TimeUnit.MILLISECONDS                // Time Unit, for example, TimeUnit.MILLISECONDS
    );

    com.google.android.gms.common.api.Status heightInsertStatus =
            Fitness.HistoryApi.insertData(mGoogleApiFitnessClient, heightDataSet)
                    .await(1, TimeUnit.MINUTES);
    if(heightInsertStatus.isSuccess()){
        Log.e("Height","Inserted");
    }
    else{
        Log.e("Height","inserted failed");
    }
}

public  void saveUserWeight(float weight) {
    // to post data
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.DAY_OF_YEAR, -1);
    long startTime = cal.getTimeInMillis();

    DataSet weightDataSet = createDataForRequest(
            DataType.TYPE_WEIGHT,    // for height, it would be DataType.TYPE_HEIGHT
            DataSource.TYPE_RAW,
            weight,                  // weight in kgs
            startTime,              // start time
            endTime,                // end time
            TimeUnit.MILLISECONDS                // Time Unit, for example, TimeUnit.MILLISECONDS
    );

    com.google.android.gms.common.api.Status weightInsertStatus =
            Fitness.HistoryApi.insertData(mGoogleApiFitnessClient, weightDataSet)
                    .await(1, TimeUnit.MINUTES);
    if(weightInsertStatus.isSuccess()){
        Log.e("Weight","Inserted");
    }
    else{
        Log.e("Weight","inserted failed");
    }
}

为了获取今天的步数,我使用了这种方法。但是对于卡路里信息,我没有找到方法..所以如果有人知道请在这里分享..

 private void getStepsToday() {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    long startTime = cal.getTimeInMillis();

    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;

    for (DataPoint dp : stepData.getDataPoints()) {
        for(Field field : dp.getDataType().getFields()) {
            int steps = dp.getValue(field).asInt();

            totalSteps += steps;

        }
    }

    publishTodaysStepData(totalSteps);
}

最佳答案

对于卡路里,您需要更改数据类型 即:

DataType.TYPE_CALORIES_EXPENDED

或者:

DailyTotalResult result = Fitness.HistoryApi.readDailyTotal(mGoogleApiClient,DataType.TYPE_CALORIES_EXPENDED).await(1,TimeUnit.MINUTES);

关于android - 如何从 google fit 读取消耗卡路里数和剩余卡路里数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32780263/

相关文章:

android - 在 android XML 上结合多个背景?

java - 日历开始日期早于结束日期的同一天差异

java - 从 Google Fit API 获取数据时不显示瞬时读数

java - google api 错误 - 无法让 google 登录正常工作

Android如何拥有带有不透明元素的透明容器

android - 在外部单击时如何关闭 PopupWindow?

java - 无法连接到 Google Play 服务;获取取消状态代码

android - 通过 Google Fit Android API 检索的每周步数与 Google Fit 官方应用中显示的步数不匹配

android - 将体重值保存到 Google Fit 时为 "Application needs OAuth consent from the User"

android - 使用来自 url 的图像折叠工具栏?