GoalsApi

public interfaceGoalsApi

This interface is deprecated.
For reading and writing historical fitness data, useHealth Connect instead.

API for reading fitnessGoalscreated by users in Google Fit.

The readCurrentGoals(GoogleApiClient, GoalsReadRequest)method should be used whenever goals are needed.

The Goals API should be accessed via theFitnessentry point. Example:

GoogleApiClient client = new GoogleApiClient.Builder(context)
.addApi(Fitness.GOALS_API)
.addScope(Fitness.SCOPE_ACTIVITY_READ)
.addScope(Fitness.SCOPE_LOCATION_READ)
...
.build();
client.connect();

PendingResult<GoalsResult> pendingResult =
Fitness.GoalsApi.readCurrentGoals(
client,
new GoalsReadRequest.Builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA)
.addDataType(DataType.TYPE_DISTANCE_DELTA)
.build());

GoalsResult readDataResult = pendingResult.await();
List<Goal> goals = readDataResult.getGoals();

This API can be combined with a subscription in theRecording Apito collect goal progress data in the background and query it later for displaying. A simple progress query example for a step metric goal:

Calendar current = Calendar.getInstance();
PendingResult<DataReadResult> pendingResult = Fitness.HistoryApi.readData(
client,
new DataReadRequest.Builder()
.read(DataType.TYPE_STEP_COUNT_DELTA)
.setTimeRange(
goal.getStartTime(current, TimeUnit.NANOSECONDS),
goal.getEndTime(current, TimeUnit.NANOSECONDS),
TimeUnit.NANOSECONDS)
.build());
DataReadResult stepReadResult = pendingResult.await();
List<DataPoint> dataPoints =
stepReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA).getDataPoints();

int total = 0;
for (DataPoint dataPoint: dataPoints) {
total += dataPoint.getValue(Field.FIELD_STEPS).asInt();
}
double progress = total / goal.getMetricObjective().getValue();

Public Method Summary

abstractPendingResult<GoalsResult>
readCurrentGoals(GoogleApiClient client, GoalsReadRequestrequest)
Reads current goals from the user's Google Fit store.

Public Methods

public abstractPendingResult<GoalsResult> readCurrentGoals(GoogleApiClient client,GoalsReadRequest request)

Reads current goals from the user's Google Fit store.

Parameters
client An existing GoogleApiClient. It does not need to be connected at the time of this call, but the read operation will be delayed until the connection is complete.
request
Returns
  • A pending result containing current goals.