Get current day's steps during datapointListener google Fit

北慕城南 提交于 2020-03-02 07:50:26

问题


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

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.

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?


回答1:


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

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

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.




回答2:


  1. Detect whether app is opened for the first time. If true, store the step value shown for the first time in the onDataPoint method. This step data has info of steps recorded by google fit previously (through other apps or google fit itself). Store the value in shared preference.
  2. Then subtract the stored steps from the current steps shown. This is equal to current days steps. //For daily use
  3. Detect whether the day has changed since the last time the app was opened.
  4. If so then, use the readData api of googlefit for the period of 1 day and get the steps.
  5. Add these steps to the stored steps. Thus the stored steps is incremented with previous day's step values.
  6. In onDataPoint subtract the stored steps value from the steps value shown.

Note: you have to account for the app not being opened for few days. Google fit should still be recording the steps. The stored steps should add up the steps of all the days the app was not opened.



来源:https://stackoverflow.com/questions/37771616/get-current-days-steps-during-datapointlistener-google-fit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!