获取datapointListener谷歌健身期间的当前步骤 [英] Get current day's steps during datapointListener google Fit

查看:86
本文介绍了获取datapointListener谷歌健身期间的当前步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,自开始记录值以来,我得到了累积的步骤.但是我只想显示当天的步数,而不是累计的步数.

Using the following code, I get the cumulative steps since I started recording the values. But I would like to show only the current day's steps instead of cumulative.

@Override
public void onConnected(@Nullable Bundle bundle) {
    DataSourcesRequest dataSourceRequest = new DataSourcesRequest.Builder()
            .setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
            .setDataSourceTypes(DataSource.TYPE_RAW)
            .build();

    ResultCallback<DataSourcesResult> dataSourcesResultCallback = new ResultCallback<DataSourcesResult>() {
        @Override
        public void onResult(DataSourcesResult dataSourcesResult) {
            for( DataSource dataSource : dataSourcesResult.getDataSources() ) {
                if(DataType.TYPE_STEP_COUNT_CUMULATIVE.equals(dataSource.getDataType())) {
                    registerStepsDataListener(dataSource, DataType.TYPE_STEP_COUNT_CUMULATIVE);
                }
            }
        }
    };

    Fitness.SensorsApi.findDataSources(googleApiClient, dataSourceRequest)
            .setResultCallback(dataSourcesResultCallback);

    Fitness.RecordingApi.subscribe(googleApiClient, DataType.TYPE_STEP_COUNT_DELTA)
            .setResultCallback(subscribeResultCallback);
}

//onDataPointListener
@Override
public void onDataPoint(DataPoint dataPoint) {
    for( final Field field : dataPoint.getDataType().getFields() ) {
        final Value value = dataPoint.getValue( field );
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvSteps.setText("Field: " + field.getName() + " Value: " + value.toString());
            }
        });
    }
}

我尝试替换dataType常量,但这没有用.在DataPointListener中使用History API调用readData方法失败了.当您获得所需的输出时,它会滞后很多,因此不应执行此操作.

I tried replacing the dataType constants, but that did not work. Calling a readData method using the History API, in the DataPointListener failed miserably. While you get the desired output, it lags a lot and one should not do such a thing.

另一种方法是更改​​日期,我在午夜获取累积步骤的数据,然后将其存储在SharedPreferences中.然后从显示的累计中减去onDatapoint期间的数字.

Another method is on day change, I get the data for the cumulative steps at midnight and then store this in SharedPreferences. Then subtract this number during the onDatapoint from the cumulative shown.

但是有没有比这更好的方法了?

But is there a better method than this?

推荐答案

基于步骤2:使用Google健身历史记录API显示今天的数据,

Based from Step 2: Displaying Data for Today in Using the Google Fit History API, Google Fit for Android: History API mentions about:

开发人员拥有的一个常见用例是当天需要的数据.幸运的是,Google通过添加Fitness.HistoryApi.readDailyTotal调用使此操作变得容易,该调用创建了一个DailyTotalResult对象,其中包含当天收集的数据.

One common use case developers have is needing data for the current day. Luckily, Google has made this easy by adding the Fitness.HistoryApi.readDailyTotal call, which creates a DailyTotalResult object containing data collected for the day.

注销用户当天操作的示例代码:

Sample code which logs out the user's step for the day:

private void displayStepDataForToday() {
    DailyTotalResult result = Fitness.HistoryApi.readDailyTotal( mGoogleApiClient, DataType.TYPE_STEP_COUNT_DELTA ).await(1, TimeUnit.MINUTES);
    showDataSet(result.getTotal());
}

您也可以尝试此SO帖子中提供的解决方案-

You can also try the solution given in this SO post - How to get step count from Google Fit REST API like Google Fit app?. I hope that works for you.

这篇关于获取datapointListener谷歌健身期间的当前步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆