问题
Pardon noobiness, I am completely new to Android, and Google APIs. I have the following code that connects to GoogleFit. I also have an API key and Oauth.
Where/how do i use API key and Oauth? Lots of guides on how to obtain them but zero info on where to put them/how to use them in the app.
And how do I actually use the steps returned. I set up a global:
private int steps;
and then try to set it via:
steps = (int)total;
but it does nothing.
Here is the rest of the function. How do I actually get the step count out of it.
private void accessGoogleFit() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
long endtime = cal.getTimeInMillis();
cal.add(Calendar.YEAR, -1);
long starttime = cal.getTimeInMillis();
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();
Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener(new OnSuccessListener<DataSet>() {
@Override
public void onSuccess(DataSet dataSet) {
Log.d("Status", "Success");
long total = dataSet.isEmpty()
? 0
: dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
Log.d("Steps ", String.valueOf(total));
steps = (int)total; //Trying to get steps here
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("Status", "Failure", e);
}
})
.addOnCompleteListener(new OnCompleteListener<DataSet>() {
@Override
public void onComplete(@NonNull Task<DataSet> task) {
Log.d("Status", "Complete");
}
});
}
Ive been up and down the official documentation and StackOverflow. But it seems like google made big changes to the API last year and so most things are outdated, including google's own tutorials (posted in 2015). And a few places that have updated documentation provide snippets of code, and I have no idea how to use them or where to put them.
回答1:
This is how i make my request Google Fit API : request with FitnessOptions like
FitnessOptions fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build();
you will need to request GoogleSignIn.requestPermissions
And my request function
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
// .read(DataType.TYPE_STEP_COUNT_DELTA)
.bucketByTime(8, TimeUnit.DAYS)
.enableServerQueries()
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
Fitness.getHistoryClient(
this,
GoogleSignIn.getLastSignedInAccount(this))
.readData(readRequest)
.addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
@Override
public void onSuccess(DataReadResponse dataReadResponse) {
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.toString());
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getStatus());
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getDataSet(DataType.TYPE_STEP_COUNT_DELTA));
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getBuckets().get(0));
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getBuckets().get(0).getDataSets().size());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("TAG_F", "onFailure: 1 " + e.getMessage());
}
})
.addOnCompleteListener(new OnCompleteListener<DataReadResponse>() {
@Override
public void onComplete(@NonNull Task<DataReadResponse> task) {
Log.d("TAG_F", "onComplete: 1 ");
}
});
来源:https://stackoverflow.com/questions/55019490/how-do-i-get-the-steps-from-google-fit-api