我试图在google fit历史记录中插入体脂百分比,但insertStatus.isSuccess()
返回false,下面是我的代码
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.HOUR_OF_DAY, -1);
long startTime = cal.getTimeInMillis();
// Create a data source
DataSource dataSource = new DataSource.Builder()
.setAppPackageName(this)
.setDataType(DataType.TYPE_BODY_FAT_PERCENTAGE)
.setName(TAG + " - body fat")
.setType(DataSource.TYPE_RAW)
.build();
// Create a data set
float value = 20;
DataSet dataSet = DataSet.create(dataSource);
DataPoint dataPoint = dataSet.createDataPoint()
.setTimestamp(endTime,TimeUnit.MILLISECONDS);
// .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
dataPoint.getValue(Field.FIELD_PERCENTAGE).setFloat(value);
dataSet.add(dataPoint);
在插入数据代码
下面 Log.i(TAG, "Inserting the dataset in the History API");
com.google.android.gms.common.api.Status insertStatus =
Fitness.HistoryApi.insertData(mClient, dataSet)
.await(1, TimeUnit.MINUTES);
// Before querying the data, check to see if the insertion succeeded.
if (!insertStatus.isSuccess()) {
Log.i(TAG, "There was a problem inserting the dataset.");
return null;
}
答案 0 :(得分:0)
我可以直接在数据点中添加值因为TYPE_BODY_FAT_PERCENTAGE
没有任何子类型字段。如下所示。
dataPoint.setFloatValues((Float) values)