使用Google Fit API的卡路里支出 [英] Calories expenditure using google fit api

查看:145
本文介绍了使用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;
}

这篇关于使用Google Fit API的卡路里支出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆