更新时间:2023-08-19 16:35:10
要插入数据,您需要为身高和体重创建一个新的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 ^^