我尝试从没有安装GFit应用程序的情况下从Google Fit获取数据,在该应用程序中,我正在获取当天的总操作量。我如何获得整整1天的开始时间和结束时间(即时间序列)的步骤。我想像iOS
中的健康应用程序一样检索具有日期和时间的全日历史记录。以下是我尝试不返回任何内容的代码段。如果有人对此有解决方案,那将是很大的帮助。
private void showSteps() {
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.DAY_OF_WEEK, -1);
long startTime = cal.getTimeInMillis();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.enableServerQueries()
.build();
DataReadResult dataReadResult = Fitness.HistoryApi.readData(mFitnessClient, readRequest).await(1, TimeUnit.MINUTES);
if (dataReadResult.getBuckets().size() > 0) {
Log.i(TAG, "Number of returned buckets of DataSets is: "
+ dataReadResult.getBuckets().size());
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (DataPoint dp : dataSet.getDataPoints()) {
Log.i(TAG, "Data point:");
Log.i(TAG, "\tType: " + dp.getDataType().getName());
Log.i(TAG, "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
Log.i(TAG, "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
for(Field field : dp.getDataType().getFields()) {
Log.i(TAG, "\tField: " + field.getName() +
" Value: " + dp.getValue(field));
}
}
}
}
} else if (dataReadResult.getDataSets().size() > 0) {
Log.i(TAG, "Number of returned DataSets is: "
+ dataReadResult.getDataSets().size());
for (DataSet dataSet : dataReadResult.getDataSets()) {
Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (DataPoint dp : dataSet.getDataPoints()) {
Log.i(TAG, "Data point:");
Log.i(TAG, "\tType: " + dp.getDataType().getName());
Log.i(TAG, "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
Log.i(TAG, "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
for (Field field : dp.getDataType().getFields()) {
Log.i(TAG, "\tField: " + field.getName() +
" Value: " + dp.getValue(field));
}
}
}
}
}
谢谢。