谷歌适合在Android中

时间:2017-09-13 11:52:12

标签: android google-fit google-fit-sdk

我目前正在开发与健身数据相关的Android应用程序,对于该应用程序,我需要获取Step_Count,HEART_RATE_BPM,睡眠持续时间和锻炼数据。我需要每小时获取数据,因此我使用了bucketByTime作为1小时,我使用AGGREGATE_ACTIVITY_SUMMARY获取所有活动的活动摘要数据,包括1小时。我也可以从中过滤运动数据,但由于API中列出了太多的运动类型,因此不实用.I'我在寻找替代解决方案。

        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));
            }
        }

    }

1 个答案:

答案 0 :(得分:2)

您可以参考此documentation来了解如何阅读详细数据和汇总数据。在示例代码中,您需要在此刻之前使用1周的范围设置开始日期和结束日期。 require。如果您只想要1小时,请使用cal.add(Calendar.WEEK_OF_YEAR, -1);。创建DataReadRequest实例后,使用[cal.add(Calendar.HOUR_OF_DAY, -1);](https://developer.android.com/reference/com/google/android/gms/fitness/HistoryApi.html#readData(com.google.android.gms.common.api.GoogleApiClient,com.google.android.gms.fitness.request.DataReadRequest))方法并同步等待或提供回调处理健身历史数据的方法。