问题
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 returns random or wrong data.
// get the start and end date of the urrent mobile
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.DAY_OF_YEAR, -10);
long startTime = cal.getTimeInMillis();
java.text.DateFormat dateFormat = DateFormat.getDateInstance();
//-------------steps-------------------------------
//code to get last 10 days steps
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
DataReadResult dataReadResult=null;
dataReadResult = Fitness.HistoryApi.readData(mGoogleApiClient, readRequest).await(1, TimeUnit.MINUTES);
// display data
//Used for aggregated data
if (dataReadResult.getBuckets().size() > 0) {
Log.e("History", "Number of buckets: " + dataReadResult.getBuckets().size());
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
showDataSet(dataSet);
}
}
}
else if (dataReadResult.getDataSets().size() > 0) //Used for non-aggregated data
{
Log.e("History", "Number of returned DataSets: " + dataReadResult.getDataSets().size());
for (DataSet dataSet : dataReadResult.getDataSets()) {
showDataSet(dataSet);
}
}
Please help me on how to get the steps of previous days. Or why am i getting wrong data. thanks
回答1:
Use a custom DataSource as:
DataSource ESTIMATED_STEP_DELTAS = new DataSource.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
DataReadRequest readRequest = new DataReadRequest.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.
回答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() ...
回答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
public void onSuccess(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
public void onFailure(@NonNull Exception e) {
}
});
}
this might help!. Correct it if there is something to shorten it.
来源:https://stackoverflow.com/questions/38495961/google-fit-android-get-last-10-days-data