androidgoogle-fitgoogle-fit-sdk

Get current day's steps during datapointListener google Fit


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?


Solution

  • 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.