com.google.android.gms.fitness.GoalsApi |
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(new Scope(Scopes.FITNESS_ACTIVITY_READ)) .addScope(new Scope(Scopes.FITNESS_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.getValues()[0].asInt(); } double progress = total / goal.getMetricObjective().getValue();
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Reads current goals from the user’s Google Fit store.
|
Reads current goals from the user’s Google Fit store.
Parameters | |
---|---|
client |
GoogleApiClient :
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. |
request |
GoalsReadRequest
|
Returns | |
---|---|
PendingResult<GoalsResult> |
a pending result containing current goals. |