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:
- Create a Fitness client, adding the Goals API.
GoalsClient.readCurrentGoalsto get a list of user goals.
The following example shows creating a new Fitness client and getting a list
of user goals for
TYPE_DISTANCE_DELTA data types:
Task<List<Goal>> response = Fitness.getGoalsClient(this, GoogleSignIn.getLastSignedInAccount(this)) .readCurrentGoals( new GoalsReadRequest.Builder() .addDataType(DataType.TYPE_STEP_COUNT_DELTA) .addDataType(DataType.TYPE_DISTANCE_DELTA) .build());
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(); DataReadRequest readRequest = new DataReadRequest.Builder() .read(DataType.TYPE_STEP_COUNT_DELTA) .setTimeRange( goal.getStartTime(current, TimeUnit.NANOSECONDS), goal.getEndTime(current, TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS) .build(); Task<DataReadResponse> response = Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this)).readData(readRequest);