我目前正在開發相關的健身數據的Android應用程序,該應用程序我需要得到STEP_COUNT,HEART_RATE_BPM,睡眠時間和鍛鍊data.I需要獲取數據每小時所以我使用bucketByTime爲1小時,我使用AGGREGATE_ACTIVITY_SUMMARY爲withing 1 hour.I所有活動獲取活動的彙總數據還可以過濾來自但是API中列出了不實用的,因爲有太多的運動類型的運動數據。我正在尋找替代解決方案。谷歌適合在一個小時之內得到鍛鍊數據的Android
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA,
DataType.AGGREGATE_STEP_COUNT_DELTA)
.aggregate(DataType.TYPE_HEART_RATE_BPM, DataType.AGGREGATE_HEART_RATE_SUMMARY)
.aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
.bucketByTime(1, TimeUnit.HOURS)
.setTimeRange(starttime, endtime, TimeUnit.MILLISECONDS)
.build();
DataReadResult dataReadResult = Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);
//Used for aggregated data
if (dataReadResult.getBuckets().size() > 0) {
Log.i("History", "Number of buckets: " + dataReadResult.getBuckets().size());
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
showDataSet(dataSet);
}
}
}
return null;
}
}
private void readData() {
new VerifyDataTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void showDataSet(DataSet dataSet) {
Log.e("History", "Data returned for Data type: " + dataSet.getDataType().getName());
DateFormat dateFormat = DateFormat.getDateInstance();
DateFormat timeFormat = DateFormat.getTimeInstance();
String sleepHours = "0";
for (DataPoint dp : dataSet.getDataPoints()) {
Log.i("History", "Data point:");
Log.i("History", "\tType: " + dp.getDataType().getName());
Log.i("History", "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
Log.i("History", "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
for(Field field : dp.getDataType().getFields()) {
if(dp.getOriginalDataSource().getAppPackageName().toString().contains("sleep") && field.getName().contains("duration")){
Value value = dp.getValue(field);
long h = TimeUnit.MILLISECONDS.toHours(value.asInt());
long m =TimeUnit.MILLISECONDS.toMinutes(value.asInt());
m = m - (h*60);
sleepHours =h +" h " + m + " m";
Log.i(TAG, "\tField: Sleep duration : " + sleepHours);
}
Log.i("History", "\tField: " + field.getName() +
" Value: " + dp.getValue(field));
}
}
}