Google Fit (android) Get Last 10 Days Data
I am trying to get last 10 days data from google Fit in Android (6.0) Phone. It returns current day data correctly, but when i try to fetch data for yesterday or before that, it re
Solution 1:
Use a custom DataSource as:
DataSourceESTIMATED_STEP_DELTAS=newDataSource.Builder()
.setDataType(DataType.TYPE_STEP_COUNT_DELTA)
.setType(DataSource.TYPE_DERIVED)
.setStreamName("estimated_steps")
.setAppPackageName("com.google.android.gms")
.build();
and place it in the DataReadRequest as
DataReadRequestreadRequest=newDataReadRequest.Builder()
.aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByActivityType(1, TimeUnit.SECONDS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
In this way you will get the exact or "derived" data as displayed in the Google Fit app.
Solution 2:
The only problem with your code is that your timespan doesn't start at 00:00:00 as one would expect but at an 24 hours offset to the current time. You can easily verify that if you call getStartTime() on the Buckets you receive from dataReadResult.getBuckets() ...
Solution 3:
private DataReadRequest dataReadRequest(){
long startTime = 0;
cal = Calendar.getInstance();
int diff = printDifference(calendar_show.getTime(),cal.getTime());
if (diff>0) {
cal.add(Calendar.DATE, -diff);
now = cal.getTime();
cal.setTime(now);
}
long endTime = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 0);
startTime = cal.getTimeInMillis();
if (endTime==startTime){
cal.set(Calendar.HOUR_OF_DAY, -1);
startTime = cal.getTimeInMillis();
}
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.TYPE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.enableServerQueries()
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
return readRequest;
}
to fetch steps :
public Task googlefitStpes(){
DataReadRequest readRequest = dataReadRequest();
return Fitness.getHistoryClient(getActivity(), GoogleSignIn.getLastSignedInAccount(getActivity()))
.readData(readRequest)
.addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
@Override
publicvoidonSuccess(DataReadResponse dataReadResponse) {
if (dataReadResponse.getBuckets()!=null){
if (!dataReadResponse.getBuckets().isEmpty()){
List<Bucket> bucketList = dataReadResponse.getBuckets();
if (bucketList!=null){
if (!bucketList.isEmpty()){
List<DataSet> dataSetList = bucketList.get(0).getDataSets();
if (dataSetList!=null){
if (!dataSetList.isEmpty()){
for (Field field : dataSetList.get(0).getDataPoints().get(0).getDataType().getFields()) {
GoogleFitSteps = Double.parseDouble(dataSetList.get(0).getDataPoints().get(0).getValue(field).toString());
}
}
}
}
}
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
publicvoidonFailure(@NonNull Exception e) {
}
});
}
this might help!. Correct it if there is something to shorten it.
Post a Comment for "Google Fit (android) Get Last 10 Days Data"