API检索日常跑步和步行步骤

API检索日常跑步和步行步骤

本文介绍了如何从Google Fit API检索日常跑步和步行步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个新手问题,但是我花了一天的时间才能弄清楚没有成功的原因.我在我的Android应用中使用了Google Fit API,我需要显示其一些数据,例如跑步和步行的日常步骤.我设法以时间单位显示数据(以x分钟运行).我需要以步骤为单位进行展示.

It is probably a newbie question but I have lost a one day to figure out w/o success.I'm using Google Fit API into my Android app and I need to show some of its data like running and walking daily steps. I have managed to show data but in time unit (f.e running in x min). I need to show it in steps unit.

下面的代码片段显示了如何以时间单位(以毫秒为单位)检索数据:

The snipped below code shows how I retrieved data as time unit (in milliseconds):

  DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
            .bucketByTime(1, TimeUnit.DAYS)
            .setTimeRange(start, end, TimeUnit.MILLISECONDS)
            .build();


  Fitness.HistoryApi.readData(client, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {
        @Override
        public void onResult(DataReadResult dataReadResult) {
            if (dataReadResult.getBuckets().size() > 0) {
                display.show("Bucket DataSet.size(): "
                        + dataReadResult.getBuckets().size());
               retrieveHistoryData(dataReadResult, client.getContext());
             }
        }

                            .....

   private void retrieveHistoryData(DataReadResult dataReadResult, Context context) {

    int walking = 0;
    int running = 0;
    int biking = 0;

    for (Bucket bucket : dataReadResult.getBuckets()) {
        List<DataSet> dataSets = bucket.getDataSets();
        for (DataSet dataSet : dataSets) {
            display.show("dataSet.dataType: " + dataSet.getDataType().getName());
            for (DataPoint dp : dataSet.getDataPoints()) {
                 walking += getBucketData(dp, FitnessActivities.WALKING);
                 running += getBucketData(dp, FitnessActivities.RUNNING);
                 biking += getBucketData(dp, FitnessActivities.BIKING);
            }
        }
    }

    Log.d(TAG, Constants.KEY_WALKING_STEPS + ": " + walking);
    Log.d(TAG, Constants.KEY_RUNNING_STEPS + ": " + running);
    Log.d(TAG, Constants.KEY_BIKING_STEPS + ": " + biking);
}

private int getBucketData(DataPoint dp, String activityName) {
    int count = 0;
    for (Field field : dp.getDataType().getFields()) {
        Log.d(TAG, "Field " + field.getName() +  " " + FitnessActivities.getName(dp.getValue(field).asInt()) + " = " + dp.getValue(field).asInt() + " field.describeContents()" + field.describeContents());
        if ("activity".equals(field.getName()) && activityName.contentEquals(FitnessActivities.getName(dp.getValue(field).asInt()))) {
            count +=  dp.getValue(field).asInt();

        }
    }
    return count;
}

提前Tnx.

最诚挚的问候

MB

推荐答案

您可以使用以下代码获取每天进行各种活动的步骤

You can get steps for various activities for each day by using the following code

DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
            .bucketByActivityType(1, TimeUnit.SECONDS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

其中ESTIMATED_STEP_DELTAS定义为

where ESTIMATED_STEP_DELTAS is defined as

DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
            .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
            .setType(DataSource.TYPE_DERIVED)
            .setStreamName("estimated_steps")
            .setAppPackageName("com.google.android.gms")
            .build();

您可以循环运行此代码以获取所需的任意天的数据.它将返回所有涉及步骤的活动的数据存储桶,您可以通过此方法将存储桶中的活动名称获取为String

You can run this code in a loop to get data for as many days as you want. It will return data buckets for all activities that have steps involved and you can fetch activity name from the bucket by this method that returns activity name as String

bucket.getActivity();

希望有帮助! :)

这篇关于如何从Google Fit API检索日常跑步和步行步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:46