本文介绍了在GoogleFit中添加/读取体重和身高?安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

通过Google,我得到了这段代码,用于插入 DataType.TYPE_STEP_COUNT_DELTA .但是如何使用Android

By Google, I got this code to insert DataType.TYPE_STEP_COUNT_DELTA. but how to insert TYPE_HEIGHT AND TYPE_WEIGHT using Android

 com.google.android.gms.common.api.Status insertStatus =
                    Fitness.HistoryApi.insertData(mClient, dataSet)
                            .await(1, TimeUnit.MINUTES);

推荐答案

要插入数据,您需要为身高和体重创建一个新的DataSet对象.

To insert data, you need to create a new DataSet object for both height and weight.

我创建了一个方法,以获取带有请求所需参数的DataSet对象.

I have created a method in order to get a DataSet object with the necessary parameters for a request.

/**
 * This method creates a dataset object to be able to insert data in google fit
 * @param dataType DataType Fitness Data Type object
 * @param dataSourceType int Data Source Id. For example, DataSource.TYPE_RAW
 * @param values Object Values for the fitness data. They must be int or float
 * @param startTime long Time when the fitness activity started
 * @param endTime long Time when the fitness activity finished
 * @param timeUnit TimeUnit Time unit in which period is expressed
 * @return
 */
private DataSet createDataForRequest(DataType dataType, int dataSourceType, Object values,
                                     long startTime, long endTime, TimeUnit timeUnit) {
    DataSource dataSource = new DataSource.Builder()
            .setAppPackageName(mAppId)
            .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;
}

然后,您需要执行以下操作:

Then, you need to do something like this:

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

    com.google.android.gms.common.api.Status weightInsertStatus =
            Fitness.HistoryApi.insertData(mClient, weightDataSet )
                    .await(1, TimeUnit.MINUTES);

如果您阅读 Google Fit文档,这将非常有帮助.在这里,您可以阅读有关数据类型

It is very helpful if you read Google Fit doc. There, you can read more information about data types

我希望这对您有帮助^^

I hope this helps ^^

这篇关于在GoogleFit中添加/读取体重和身高?安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:09