使用Google Fit API的卡路里支出

使用Google Fit API的卡路里支出

问题描述:

我正在使用App Fitness应用,因为我使用了google fit api.到目前为止,我已经成功地获取了步数,距离,但是我无法获得卡路里的消耗. 在此先感谢

I am working on app fitness app , for that I used google fit api . Till now I am successful in fetching steps count , distance but I am unable to get calorie expenditure . Thanks in Advance

您需要先设置用户的体重和身高.使用此信息可以计算出消耗的卡路里.

You need to set the user's weight and height first. The expended calories are calculated using this information.

这些是我用来执行此操作的方法. (mClient是GoogleApiClient实例)

These are the methods that I use to do this. (mClient is a GoogleApiClient instance)

public static 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(mClient, heightDataSet)
                    .await(1, TimeUnit.MINUTES);
}

public static 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(mClient, weightDataSet)
                    .await(1, TimeUnit.MINUTES);
}

public static DataSet createDataForRequest(DataType dataType,
                                           int dataSourceType,
                                           Object values,
                                           long startTime,
                                           long endTime,
                                           TimeUnit timeUnit) {
    DataSource dataSource = new DataSource.Builder()
            .setAppPackageName(appContext)
            .setDataType(dataType)
            .setType(dataSourceType)
            .build();

    DataSet dataSet = DataSet.create(dataSource);
    DataPoint dataPoint = dataSet.createDataPoint().setTimeInterval(startTime, endTime, timeUnit);

    if (values instanceof Integer) {
        dataPoint = dataPoint.setIntValues((Integer) values);
    } else {
        dataPoint = dataPoint.setFloatValues((Float) values);
    }

    dataSet.add(dataPoint);

    return dataSet;
}