Work with Fitness Goals

Goals provide a way for Google Fit users to track their health and fitness progress. The Goals API enables your app to read user goals, and use this data to enhance the user's experience by providing updates and insights related to the user's progress.

Read goals data

To get a list of user goals:

The following example shows creating a new Fitness client and getting a list of user goals for TYPE_STEP_COUNT_DELTA and TYPE_DISTANCE_DELTA data types:

GoogleApiClient client = new GoogleApiClient.Builder(context)
     .addApi(Fitness.GOALS_API)
     .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
     .addScope(new Scope(Scopes.FITNESS_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();

Once you've got the user's goals data, you can use the History API to query for progress. The following example demonstrates reading the user's step data and calculating their current progress toward their step goal (it is assumed that your app has already used the Recording API to subscribe to the TYPE_STEP_COUNT_DELTA data type):

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.getValues()[0].asInt();
}
double progress = total / goal.getMetricObjective().getValue();

Send feedback about...