GoalsApi
Stay organized with collections
Save and categorize content based on your preferences.
This interface is deprecated.
For reading and writing historical fitness data, use Health Connect
instead.
API for reading fitness Goals
created
by users in Google Fit.
The readCurrentGoals(GoogleApiClient, GoalsReadRequest)
method should be used whenever
goals are needed.
The Goals API should be accessed via the Fitness
entry
point. Example:
GoogleApiClient client = new GoogleApiClient.Builder(context)
.addApi(Fitness.GOALS_API)
.addScope(Fitness.SCOPE_ACTIVITY_READ)
.addScope(Fitness.SCOPE_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();
This API can be combined with a subscription in the Recording
Api
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();
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.getValue(Field.FIELD_STEPS).asInt();
}
double progress = total / goal.getMetricObjective().getValue();
Public Methods
Reads current goals from the user's Google Fit store.
Parameters
Returns
- A pending result containing current goals.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[[["\u003cp\u003eThe GoalsApi is deprecated; use Health Connect for historical fitness data instead.\u003c/p\u003e\n"],["\u003cp\u003eThis API was used to read fitness goals created by users in Google Fit.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ereadCurrentGoals()\u003c/code\u003e was the primary method for retrieving goals.\u003c/p\u003e\n"],["\u003cp\u003eThe Goals API could be combined with the Recording API to track goal progress.\u003c/p\u003e\n"]]],["The Goals API is deprecated; use Health Connect instead for historical fitness data. To access goals, use `Fitness.GoalsApi.readCurrentGoals()` with a `GoogleApiClient` and `GoalsReadRequest`. This method retrieves user-created goals from Google Fit. Goal progress can be tracked by combining the API with the Recording API and querying data via the `HistoryApi`, as shown in the provided steps example. `readCurrentGoals` retrieves the result of current goals via a `PendingResult` object.\n"],null,["# GoalsApi\n\npublic interface **GoalsApi** \n**This interface is deprecated.** \n\nFor reading and writing historical fitness data, use [Health Connect](//developer.android.com/guide/health-and-fitness/health-connect)\ninstead.\n\nAPI for reading fitness [Goals](/android/reference/com/google/android/gms/fitness/data/Goal) created\nby users in Google Fit.\n\nThe [readCurrentGoals(GoogleApiClient, GoalsReadRequest)](/android/reference/com/google/android/gms/fitness/GoalsApi#readCurrentGoals(com.google.android.gms.common.api.GoogleApiClient,%20com.google.android.gms.fitness.request.GoalsReadRequest)) method should be used whenever\ngoals are needed.\n\nThe Goals API should be accessed via the [Fitness](/android/reference/com/google/android/gms/fitness/Fitness) entry\npoint. Example: \n\n```\n GoogleApiClient client = new GoogleApiClient.Builder(context)\n .addApi(Fitness.GOALS_API)\n .addScope(Fitness.SCOPE_ACTIVITY_READ)\n .addScope(Fitness.SCOPE_LOCATION_READ)\n ...\n .build();\n client.connect();\n\n PendingResult\u003cGoalsResult\u003e pendingResult =\n Fitness.GoalsApi.readCurrentGoals(\n client,\n new GoalsReadRequest.Builder()\n .addDataType(DataType.TYPE_STEP_COUNT_DELTA)\n .addDataType(DataType.TYPE_DISTANCE_DELTA)\n .build());\n\n GoalsResult readDataResult = pendingResult.await();\n List\u003cGoal\u003e goals = readDataResult.getGoals();\n \n```\n\nThis API can be combined with a subscription in the [Recording\nApi](/android/reference/com/google/android/gms/fitness/RecordingApi) to collect goal progress data in the background and query it later for\ndisplaying. A simple progress query example for a step metric goal: \n\n```\n Calendar current = Calendar.getInstance();\n PendingResult\u003cDataReadResult\u003e pendingResult = Fitness.HistoryApi.readData(\n client,\n new DataReadRequest.Builder()\n .read(DataType.TYPE_STEP_COUNT_DELTA)\n .setTimeRange(\n goal.getStartTime(current, TimeUnit.NANOSECONDS),\n goal.getEndTime(current, TimeUnit.NANOSECONDS),\n TimeUnit.NANOSECONDS)\n .build());\n DataReadResult stepReadResult = pendingResult.await();\n List\u003cDataPoint\u003e dataPoints =\n stepReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA).getDataPoints();\n\n int total = 0;\n for (DataPoint dataPoint : dataPoints) {\n total += dataPoint.getValue(Field.FIELD_STEPS).asInt();\n }\n double progress = total / goal.getMetricObjective().getValue();\n \n``` \n\n### Public Method Summary\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [PendingResult](/android/reference/com/google/android/gms/common/api/PendingResult)\\\u003c[GoalsResult](/android/reference/com/google/android/gms/fitness/result/GoalsResult)\\\u003e | [readCurrentGoals](/android/reference/com/google/android/gms/fitness/GoalsApi#readCurrentGoals(com.google.android.gms.common.api.GoogleApiClient,%20com.google.android.gms.fitness.request.GoalsReadRequest))([GoogleApiClient](/android/reference/com/google/android/gms/common/api/GoogleApiClient) client, [GoalsReadRequest](/android/reference/com/google/android/gms/fitness/request/GoalsReadRequest) request) Reads current goals from the user's Google Fit store. |\n\nPublic Methods\n--------------\n\n#### public abstract [PendingResult](/android/reference/com/google/android/gms/common/api/PendingResult)\\\u003c[GoalsResult](/android/reference/com/google/android/gms/fitness/result/GoalsResult)\\\u003e\n**readCurrentGoals** ([GoogleApiClient](/android/reference/com/google/android/gms/common/api/GoogleApiClient) client, [GoalsReadRequest](/android/reference/com/google/android/gms/fitness/request/GoalsReadRequest) request)\n\nReads current goals from the user's Google Fit store. \n\n##### Parameters\n\n| client | An existing GoogleApiClient. It does not need to be connected at the time of this call, but the read operation will be delayed until the connection is complete. |\n| request | |\n|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- A pending result containing current goals."]]