GoalsClient
Stay organized with collections
Save and categorize content based on your preferences.
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();
Inherited Method Summary
From class com.google.android.gms.common.api.GoogleApi
getApiFallbackAttributionTag
( Context
arg0)
From class java.lang.Object
Public Methods
Reads current goals from the user's Google Fit store.
Returns
-
Task
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\u003e\u003ccode\u003eGoalsClient\u003c/code\u003e is deprecated and developers should use Health Connect for historical fitness data.\u003c/p\u003e\n"],["\u003cp\u003eThis client 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 accessing fitness goals.\u003c/p\u003e\n"],["\u003cp\u003eAccess the \u003ccode\u003eGoalsClient\u003c/code\u003e through the \u003ccode\u003eFitness\u003c/code\u003e entry point with appropriate fitness options and GoogleSignInAccount.\u003c/p\u003e\n"]]],[],null,["# GoalsClient\n\npublic class **GoalsClient** extends [GoogleApi](/android/reference/com/google/android/gms/common/api/GoogleApi)\\\u003c[Api.ApiOptions.HasGoogleSignInAccountOptions](/android/reference/com/google/android/gms/common/api/Api.ApiOptions.HasGoogleSignInAccountOptions)\\\u003e \n**This class is deprecated.** \n\nFor reading and writing historical fitness data, use [Health Connect](//developer.android.com/guide/health-and-fitness/health-connect)\ninstead.\n\nClient for reading fitness [Goals](/android/reference/com/google/android/gms/fitness/data/Goal) created\nby users in Google Fit.\n\nThe [readCurrentGoals(GoalsReadRequest)](/android/reference/com/google/android/gms/fitness/GoalsClient#readCurrentGoals(com.google.android.gms.fitness.request.GoalsReadRequest)) method should be used whenever goals are\nneeded.\n\nThe Goals Client should be accessed via the [Fitness](/android/reference/com/google/android/gms/fitness/Fitness) entry\npoint. Example: \n\n```\n\n GoogleSignInOptionsExtension fitnessOptions =\n FitnessOptions.builder()\n .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)\n .addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)\n .build();\n\n GoogleSignInAccount googleSignInAccount =\n GoogleSignIn.getAccountForExtension(this, fitnessOptions);\n\n Task\u003cList\u003cGoal\u003e\u003e response = Fitness.getGoalsClient(this, googleSignInAccount)\n .readCurrentGoals(new GoalsReadRequest.Builder()\n .addDataType(DataType.TYPE_STEP_COUNT_DELTA)\n .addDataType(DataType.TYPE_DISTANCE_DELTA)\n .build());\n\n List\u003cGoal\u003e goals = Tasks.await(response);\n \n```\n\nThis Client can be combined with a subscription in the [Recording\nClient](/android/reference/com/google/android/gms/fitness/RecordingClient) 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 Task\u003cDataReadResponse\u003e response = Fitness.getHistoryClient(this, googleSignInAccount)\n .readData(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 DataReadResponse stepReadResponse = Tasks.await(response);\n List\u003cDataPoint\u003e dataPoints =\n stepReadResponse.getDataSet(DataType.TYPE_STEP_COUNT_DELTA).getDataPoints();\n\n int total = 0;\n for (DataPoint dataPoint : dataPoints) {\n total += dataPoint.getValues()[0].asInt();\n }\n double progress = total / goal.getMetricObjective().getValue();\n \n``` \n\n### Public Method Summary\n\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Task](/android/reference/com/google/android/gms/tasks/Task)\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Goal](/android/reference/com/google/android/gms/fitness/data/Goal)\\\u003e\\\u003e | [readCurrentGoals](/android/reference/com/google/android/gms/fitness/GoalsClient#readCurrentGoals(com.google.android.gms.fitness.request.GoalsReadRequest))([GoalsReadRequest](/android/reference/com/google/android/gms/fitness/request/GoalsReadRequest) request) Reads current goals from the user's Google Fit store. |\n\n### Inherited Method Summary\n\nFrom class com.google.android.gms.common.api.GoogleApi \n\n|-------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|\n| [String](//developer.android.com/reference/java/lang/String.html) | getApiFallbackAttributionTag([Context](//developer.android.com/reference/android/content/Context.html) arg0) |\n\nFrom class java.lang.Object \n\n|----------------------------------------------------------------------------|--------------------------------------------------------------------------------|\n| [Object](//developer.android.com/reference/java/lang/Object.html) | clone() |\n| boolean | equals([Object](//developer.android.com/reference/java/lang/Object.html) arg0) |\n| void | finalize() |\n| final [Class](//developer.android.com/reference/java/lang/Class.html)\\\u003c?\\\u003e | getClass() |\n| int | hashCode() |\n| final void | notify() |\n| final void | notifyAll() |\n| [String](//developer.android.com/reference/java/lang/String.html) | toString() |\n| final void | wait(long arg0, int arg1) |\n| final void | wait(long arg0) |\n| final void | wait() |\n\nPublic Methods\n--------------\n\n#### public [Task](/android/reference/com/google/android/gms/tasks/Task)\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Goal](/android/reference/com/google/android/gms/fitness/data/Goal)\\\u003e\\\u003e\n**readCurrentGoals** ([GoalsReadRequest](/android/reference/com/google/android/gms/fitness/request/GoalsReadRequest) request)\n\nReads current goals from the user's Google Fit store. \n\n##### Returns\n\n- [Task](/android/reference/com/google/android/gms/tasks/Task) containing current goals."]]