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?