public class

GoalsClient

extends GoogleApi<FitnessOptions>
java.lang.Object
   ↳ com.google.android.gms.common.api.GoogleApi<com.google.android.gms.fitness.FitnessOptions>
     ↳ com.google.android.gms.fitness.GoalsClient

Class Overview

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 extension =
      FitnessOptions.builder()
          .addDataType(DataTypes.TYPE_STEP_COUNT_DELTA, FitnessOptions.READ)
          .addDataType(DataTypes.TYPE_DISTANCE_DELTA, FitnessOptions.READ)
          .build()
    GoogleSignInOptions signInOptions =
      new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
          .addExtension(extension)
          .build();
    Task<GoogleSignInAccount> task = GoogleSignIn.getClient(this, signInOptions)
        .silentSignIn();
    GoogleSignInAccount googleSigninAccount = Tasks.await(task);

    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();
 

Summary

Public Methods
Task<List<Goal>> readCurrentGoals(GoalsReadRequest request)
Reads current goals from the user’s Google Fit store.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

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

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

Parameters
request GoalsReadRequest
Returns
Task<List<Goal>> Task containing current goals.