GoalsClient

public class GoalsClient extends GoogleApi<Api.ApiOptions.HasGoogleSignInAccountOptions>

This class is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Client for reading fitness Goals created by users in Google Fit.

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

The Goals Client should be accessed via the Fitness entry point. Example:


    GoogleSignInOptionsExtension fitnessOptions =
        FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
            .build();

    GoogleSignInAccount googleSignInAccount =
        GoogleSignIn.getAccountForExtension(this, fitnessOptions);

    Task<List<Goal>> response = Fitness.getGoalsClient(this, googleSignInAccount)
        .readCurrentGoals(new GoalsReadRequest.Builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA)
            .addDataType(DataType.TYPE_DISTANCE_DELTA)
            .build());

    List<Goal> goals = Tasks.await(response);
 

This Client can be combined with a subscription in the Recording Client to 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();
    Task<DataReadResponse> response = Fitness.getHistoryClient(this, googleSignInAccount)
        .readData(new DataReadRequest.Builder()
            .read(DataType.TYPE_STEP_COUNT_DELTA)
            .setTimeRange(
                goal.getStartTime(current, TimeUnit.NANOSECONDS),
                goal.getEndTime(current, TimeUnit.NANOSECONDS),
                TimeUnit.NANOSECONDS)
            .build());
    DataReadResponse stepReadResponse = Tasks.await(response);
    List<DataPoint> dataPoints =
        stepReadResponse.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();
 

Public Method Summary

Task<List<Goal>>
readCurrentGoals(GoalsReadRequest request)
Reads current goals from the user's Google Fit store.

Inherited Method Summary

Public Methods

public Task<List<Goal>> readCurrentGoals (GoalsReadRequest request)

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

Returns
  • Task containing current goals.