How do I access Google Fit for users on Google Play Services?
I am showing daily custom stride data and activity time in android app using Services. I got the API setup ok and it all works fine, but I would also like to read the AIM user (the one set in the Google Fit profile) for daily steps and activity times so that I can show the percentage reached. How can I achieve this? I cannot find any of the APIs at com.google.android.gms.fitness. * Suggesting it.
Thank,
Michael
source to share
Recent changes to android game services (9.8.0) are supposed to make this possible.
1. Add Fitness.GOALS_API to GoogleAPiClient
googleApiClient = new GoogleApiClient.Builder(CrossoverWatchFaceService.this)
.addConnectionCallbacks([...])
.addApi(Fitness.HISTORY_API)
.addApi(Fitness.GOALS_API)
.useDefaultAccount()
.build();
googleApiClient.connect();
2.Set goal (s)
PendingResult<GoalsResult> pendingResult =
Fitness.GoalsApi.readCurrentGoals(
googleApiClient,
new GoalsReadRequest.Builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA)
.build());
pendingResult.setResultCallback(
new ResultCallbacks<GoalsResult>() {
@Override
public void onSuccess(@NonNull GoalsResult goalsResult) {
List<Goal> goals = goalsResult.getGoals();
//YOUR CODE HERE
}
@Override
public void onFailure(@NonNull Status status) {
Log.d(TAG, "onFailure: ");
}
});
https://developers.google.com/fit/android/using-goals
and
https://developers.google.com/android/reference/com/google/android/gms/fitness/GoalsApi
source to share
If you want to use sync call use below code snippet for second part. Extract the target:
PendingResult<GoalsResult> pendingResult = Fitness.GoalsApi.readCurrentGoals(
googleApiClient, new GoalsReadRequest.Builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA)
.build());
GoalsResult goalsResult = pendingResult.await();
List<Goal> goals = goalsResult.getGoals();
// assume 1st goal is step count
double stepGoal = goals.get(0).getMetricObjective().getValue();
source to share