我正在使用 Google Fit 构建一个健身应用程序,我遇到的问题是,当我通过历史 API 发送数据时,无论我使用插入方法还是更新方法。数据有时只会在 Google 本身的 Fit 应用程序中立即更改。这意味着当我通过我自己的应用程序中的历史 API 插入数据然后将其发送给 Google 时。Google 的 Fit 应用程序有时只会立即更改相应的数据。当我从我的 Google 帐户注销时,数据会立即更改。我的 logcat 向我显示数据已成功发送并正确到达,Google 或 Google 服务需要时间来更新数据并显示它。
导致此问题的原因是时间间隔吗?这是我从 Numberpicker 获取数据以确定高度并将其发送给 Google 的代码。我已经阅读了文档,但目前无法得出结论。关于为什么会这样的任何想法?
private DataSet updateFitnessData() {
Log.i(TAG, "Creating a new data update request.");
// [START build_update_data_request]
// Set a start and end time for the data that fits within the time range
// of the original insertion.
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
cal.add(Calendar.MINUTE, 0);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.MINUTE, 0);
long startTime = cal.getTimeInMillis();
// Create a data source
DataSource dataSource =
new DataSource.Builder()
.setAppPackageName(this)
.setDataType(DataType.TYPE_HEIGHT)
.setStreamName(TAG + " - step count")
.setType(DataSource.TYPE_RAW)
.build();
// Create a data set
DataSet dataSet = DataSet.create(dataSource);
// For each data point, specify a start time, end time, and the data value
BigDecimal bigDecimal = new BigDecimal(heightDelta);
BigDecimal changedValue = bigDecimal.movePointLeft(2);
Float heightDecimalMoved = changedValue.floatValue();
DataPoint dataPoint =
dataSet.createDataPoint().setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
dataPoint.getValue(Field.FIELD_HEIGHT).setFloat(heightDecimalMoved);
dataSet.add(dataPoint);
// [END build_update_data_request]
return dataSet;
}
private Task<Void> updateData() {
// Create a new dataset and update request.
DataSet dataSet = updateFitnessData();
long startTime = 0;
long endTime = 0;
// Get the start and end times from the dataset.
for (DataPoint dataPoint : dataSet.getDataPoints()) {
startTime = dataPoint.getStartTime(TimeUnit.MILLISECONDS);
endTime = dataPoint.getEndTime(TimeUnit.MILLISECONDS);
}
// [START update_data_request]
Log.i(TAG, "Updating the dataset in the History API.");
DataUpdateRequest request =
new DataUpdateRequest.Builder()
.setDataSet(dataSet)
.setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
// Invoke the History API to update data.
return Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.updateData(request)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// At this point the data has been updated and can be read.
Log.i(TAG, "Data update was successful.");
} else {
Log.e(TAG, "There was a problem updating the dataset.", task.getException());
}
}
});
}