问题
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 aDailyTotalResult
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:
- 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.
- Then subtract the stored steps from the current steps shown. This is equal to current days steps. //For daily use
- Detect whether the day has changed since the last time the app was opened.
- If so then, use the readData api of googlefit for the period of 1 day and get the steps.
- Add these steps to the stored steps. Thus the stored steps is incremented with previous day's step values.
- 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