Work with Fitness Goals

Goals let Google Fit users track their progress against daily targets they set for themselves. The GoalsClient lets your app read these goals and check a user's progress against their current goal. You can use this data to create a more personalized experience and motivate your users.

Reading a goal

This example shows how to create a new Fitness client and get the user's Heart Points goal (or null, if they don't have a goal set).

public Goal readHeartPointsGoal() {
  GoogleSignInOptionsExtension fitnessOptions =
          .addDataType(DataType.TYPE_HEART_POINTS, FitnessOptions.ACCESS_READ)

  Task<List<Goal>> response =
      Fitness.getGoalsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
              new GoalsReadRequest.Builder().addDataType(DataType.TYPE_HEART_POINTS).build());

  List<Goal> goals = Tasks.await(response);
  if (goals.size() == 1) {
    return goals.get(0);
  return null;

Checking progress

Once you have the user's Heart Points goal, you can use the HistoryClient to check their progress. This example shows how to check how many Heart Points the user has.

public double readHeartPointsProgress(Goal goal) {
  Calendar current = Calendar.getInstance();
  DataReadRequest request =
      new DataReadRequest.Builder()
              goal.getStartTime(current, TimeUnit.NANOSECONDS),
              goal.getEndTime(current, TimeUnit.NANOSECONDS),

    Task<DataReadResponse> task =
        Fitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))

  DataReadResponse response = Tasks.await(task);

  DataSet dataSet = response.getDataSets().get(0);
  double heartPoints =
          .mapToDouble(point -> (double) point.getValue(Field.FIELD_INTENSITY).asFloat())
  return heartPoints;

Calculating progress as a percentage

This example shows how to calculate the user's progress towards meeting their Heart Points goal. The resulting percentage indicates how much of their goal they've achieved.

Goal goal = readHeartPointsGoal();
double progress = readHeartPointsProgress(goal);
String message = String.format(
    "Heart points progress: %.1f / %.1f",
        progress, goal.getMetricObjective().getValue()));