Los objetivos son aquellos en la app de Google Fit que los usuarios pueden establecer para sí mismos. Ayudan a motivar a los usuarios a mantenerse activos todos los días. Desde su perfil, los usuarios pueden ajustar la cantidad de pasos y Puntos de frecuencia cardíaca a los que quieren apuntar cada día. La plataforma de Fit registra sus objetivos y hace un seguimiento de su actividad diaria en comparación con ellos.
Crea una mejor experiencia del usuario con objetivos
Tu app puede leer los objetivos de un usuario para hacer un seguimiento de sus objetivos personales. Esto puede ayudar a crear una experiencia más atractiva. Para comenzar, usa el cliente GoalsClient
para leer los objetivos de pasos y Puntos cardio del usuario. Luego, usa el cliente HistoryClient
para ver qué tan cerca están de esos objetivos.
Usa estos datos para ayudar a los usuarios a cambiar entre Google Fit y tu app sin inconvenientes, y obtener información coherente en ambas apps sobre el progreso hacia sus objetivos de entrenamiento.
Motiva a los usuarios a alcanzar sus objetivos diarios ofreciéndoles actualizaciones y estadísticas relacionadas con su progreso.
Cómo leer una meta
En el siguiente ejemplo, se muestra cómo crear un nuevo cliente de Fitness y obtener el objetivo de Puntos cardio del usuario, o null
si no tiene un objetivo establecido.
Kotlin
private val fitnessOptions: FitnessOptions by lazy { FitnessOptions.builder() .addDataType(DataType.TYPE_HEART_POINTS, FitnessOptions.ACCESS_READ) .build() } private val goalsReadRequest: GoalsReadRequest by lazy { GoalsReadRequest.Builder() .addDataType(DataType.TYPE_HEART_POINTS) .build() } private fun getGoogleAccount(): GoogleSignInAccount = GoogleSignIn.getAccountForExtension(requireContext(), fitnessOptions) private fun readGoals() { Fitness.getGoalsClient(requireContext(), getGoogleAccount()) .readCurrentGoals(goalsReadRequest) .addOnSuccessListener { goals -> // There should be at most one heart points goal currently. goals.firstOrNull()?.apply { // What is the value of the goal val goalValue = metricObjective.value Log.i(TAG, "Goal value: $goalValue") // How is the goal measured? Log.i(TAG, "Objective: $objective") // How often does the goal repeat? Log.i(TAG, "Recurrence: $recurrenceDetails") } } } private val Goal.objective: String get() = when (objectiveType) { OBJECTIVE_TYPE_DURATION -> "Duration (s): ${durationObjective.getDuration(TimeUnit.SECONDS)}" OBJECTIVE_TYPE_FREQUENCY -> "Frequency : ${frequencyObjective.frequency}" OBJECTIVE_TYPE_METRIC -> "Metric : ${metricObjective.dataTypeName} - ${metricObjective.value}" else -> "Unknown objective" } private val Goal.recurrenceDetails: String get() = recurrence?.let { val period = when (it.unit) { Recurrence.UNIT_DAY -> "days" Recurrence.UNIT_WEEK -> "weeks" Recurrence.UNIT_MONTH -> "months" else -> "Unknown" } "Every ${recurrence!!.count} $period" } ?: "Does not repeat"
Java
private final FitnessOptions fitnessOptions = FitnessOptions.builder() .addDataType(DataType.TYPE_HEART_POINTS, FitnessOptions.ACCESS_READ) .build(); private final GoalsReadRequest goalsReadRequest = new GoalsReadRequest.Builder() .addDataType(DataType.TYPE_HEART_POINTS) .build(); private GoogleSignInAccount getGoogleAccount() { GoogleSignIn.getAccountForExtension(getApplicationContext(), fitnessOptions); } private void readGoals() { Fitness.getGoalsClient(getApplicationContext(), getGoogleAccount()) .readCurrentGoals(goalsReadRequest) .addOnSuccessListener(goals -> { // There should be at most one heart points goal currently. Optional<Goal> optionalGoal = goals.stream().findFirst(); if (optionalGoal.isPresent()) { // What is the value of the goal double goalValue = optionalGoal.get().getMetricObjective().getValue(); Log.i(TAG, "Goal value: $goalValue"); // How is the goal measured? Log.i(TAG, "Objective: ${getObjective(optionalGoal.get())}"); // How often does the goal repeat? Log.i(TAG, "Recurrence: ${getRecurrenceDetails(optionalGoal.get())}"); } }); } private String getObjective(Goal goal) { switch (goal.getObjectiveType()) { case OBJECTIVE_TYPE_DURATION: return "Duration (s): ${goal.getDurationObjective().getDuration(TimeUnit.SECONDS)}"; case OBJECTIVE_TYPE_FREQUENCY: return "Frequency : ${goal.getFrequencyObjective().getFrequency()}"; case OBJECTIVE_TYPE_METRIC: return "Metric : ${goal.getMetricObjective().getDataTypeName()} - ${goal.getMetricObjective().getValue()}"; default: return "Unknown objective"; } } private String getRecurrenceDetails(Goal goal) { Goal.Recurrence recurrence = goal.getRecurrence(); if (recurrence == null) { return "Does not repeat"; } StringBuilder recurrenceMessage = new StringBuilder("Every ${recurrence.getCount()}"); switch (recurrence.getUnit()) { case UNIT_DAY: recurrenceMessage.append("days"); break; case UNIT_WEEK: recurrenceMessage.append("weeks"); break; case UNIT_MONTH: recurrenceMessage.append("months"); break; default: recurrenceMessage.delete(0, recurrenceMessage.length()); recurrenceMessage.append("Unknown"); break; } return recurrenceMessage.toString(); }
Cómo verificar el progreso
Cuando alcances el objetivo de Puntos cardio del usuario, puedes usar el HistoryClient
para verificar su progreso. En el siguiente ejemplo, se muestra cómo verificar cuántos Puntos cardio tiene el usuario.
Kotlin
val current = Calendar.getInstance() val request = DataReadRequest.Builder() .read(DataType.TYPE_HEART_POINTS) .setTimeRange( goal.getStartTime(current, TimeUnit.NANOSECONDS), goal.getEndTime(current, TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS ) .build() Fitness.getHistoryClient(requireContext(), getGoogleAccount()) .readData(request) .addOnSuccessListener { response -> val heartPointsSet = response.dataSets.first() val totalHeartPoints = heartPointsSet.dataPoints.sumBy { it.getValue(Field.FIELD_INTENSITY).asFloat().toInt() } Log.i(TAG, "Total heart points: $totalHeartPoints") }
Java
Calendar current = Calendar.getInstance(); DataReadRequest request = new DataReadRequest.Builder() .read(DataType.TYPE_HEART_POINTS) .setTimeRange( goal.getStartTime(current, TimeUnit.NANOSECONDS), goal.getEndTime(current, TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS ) .build(); Fitness.getHistoryClient(getApplicationContext(), getGoogleAccount()) .readData(request) .addOnSuccessListener(response -> { Optional<DataSet> heartPointsSet = response.getDataSets().stream().findFirst(); if (heartPointsSet.isPresent()) { int totalHeartPoints = 0; for (DataPoint dp : heartPointsSet.get().getDataPoints()) { totalHeartPoints += (int) dp.getValue(Field.FIELD_INTENSITY).asFloat(); } Log.i(TAG, "Total heart points: $totalHeartPoints"); } });
Calcular el progreso como un porcentaje
Si divides el total del ejemplo de verificación del progreso por el objetivo del ejemplo de leer un objetivo, puedes calcular el progreso hacia el objetivo como un porcentaje.
Kotlin
private fun calculateProgressPercentage(goal: Goal, response: DataReadResponse): Double { val goalValue = goal.metricObjective.value val currentTotal = response.dataSets.first().dataPoints.sumBy { it.getValue(Field.FIELD_INTENSITY).asFloat().toInt() } return (currentTotal.div(goalValue)).times(100.0) }
Java
private double calculateProgressPercentage(Goal goal, DataReadResponse response) { double goalValue = goal.getMetricObjective().getValue(); Optional<DataSet> firstDataSet = response.getDataSets().stream().findFirst(); if (!(firstDataSet.isPresent())) { return NaN; } double currentTotal = 0; for (DataPoint dp : firstDataSet.get().getDataPoints()) { currentTotal += (int)dp.getValue(Field.FIELD_INTENSITY).asFloat(); } return (currentTotal / goalValue) * 100.0; }