Handling Fitness Goals Using Health Services
Samsung Developer
One of the methods for obtaining data from Health Services running on Galaxy Watch with Wear OS Powered by Samsung is to register notifications for fitness goals. Galaxy Watch collects physical activity data in real-time and allows the user to sign up for information about achieving their goals. This mechanism allows you to monitor the Health Services data in the background. A sample application named GoalEvent shows the above described mechanism. Full source code of this application can be downloaded from GoalEvent.
Application overview
GoalEvent is a sample application with only one task: it is designed to monitor Health Services data in the background. Its interface allows the user to subscribe to a health event. Two buttons make it possible to set the amount of steps to achieve and a third button makes it is possible to subscribe to an event. A toast with information about achieving the goal is shown when the step count is greater than or equal to the set amount of steps.
The construction of the most important elements of this application is described in the next sections, step-by-step. The code below comes from the GoalEvent application.
Add Health Services
The first step to start using Health Services is to add the implementation of the service client to application dependencies. There are also necessary changes in the manifest file. To interact with Health Services, there should be information about this package. More information about adding Health Services usage to a project is contained in this article: Using Health Services on Galaxy Watch
Obtain permissions
The second step of the application is to obtain permissions. To use necessary operating system elements, you should place all of the required permissions in the Android manifest. There are two types of permissions needed for receiving steps information and for showing an ongoing notification.
Permissions required by the application:
<!-- For receiving steps information. -->
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<!-- For showing an ongoing notification. -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
As a next step, these permissions should be requested in the main activity of the application. After receiving the results of the permissions request and making sure that all of them are granted, it is possible to go on and check capabilities.
Request permission example code:
requestPermissions(new String[]{
Manifest.permission.FOREGROUND_SERVICE,
Manifest.permission.ACTIVITY_RECOGNITION},
0);
Check capabilities
The next step, after obtaining the permissions, that is necessary to use the health platform and making sure that all permissions have been obtained, is to check that the device can provide passive goals. This takes place in the passive monitoring client where there are data types corresponding to a chosen activity—for example, steps.
Check capabilities in goal event client:
boolean supportsStepsEvent = result
.getSupportedDataTypesEvents()
.contains(DataType.STEPS);
if(supportsStepsEvent) {
registerGoal(context);
}
Register a goal
After getting information that step goals are supported, it is possible to register a goal. To do this, you need to create a passive goal and register the appropriate callback. The callback, which is received, must be served by a specially designed interface. This class should be appropriately marked in the Android manifest as a goal receiver.
Register a goal in the goal event client:
public class GoalEventClient {
//...
public void registerGoal(Context context) {
if(goalRegistered)
return;
goalRegistered = true;
mPassiveGoal = new PassiveGoal(
new DataTypeCondition(DataType.STEPS, Value.ofLong(NUMBER_OF_STEPS), ComparisonType.GREATER_THAN_OR_EQUAL),
PassiveGoal.TriggerType.ONCE);
mPassiveMonitoringClient.registerPassiveGoalCallback(mPassiveGoal, new ComponentName(context, EventReceiver.class));
}
//...
}
Add receiver information inside of the application tag in the Android manifest:
<application
<!--....-->
<receiver android:name="com.samsung.android.goalevent.EventReceiver" android:exported="true">
<intent-filter>
<action android:name="hs.passivemonitoring.GOAL" />
</intent-filter>
</receiver>
<!--...-->
</application>
Receive notification
In the receiver code, first you need to check the intent action to ensure that it is a passive goal. Then reconstruct the passive goal from the intent to determine that the incoming goal is the same as the goal created earlier.
Override the receive method in the event receiver:
public final class EventReceiver extends BroadcastReceiver {
private static final String TAG = "EventReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if(!intent.getAction().equals(PassiveGoal.ACTION_GOAL)){
return;
}
PassiveGoal goal = PassiveGoal.fromIntent(intent);
if(goal != null && goal.equals(GoalEventClient.getInstance(context).getPassiveGoal())){
Log.i(TAG, "Step goal achieved!");
Toast.makeText(context,
R.string.step_goal_achieved,
Toast.LENGTH_LONG)
.show();
GoalEventClient.getInstance(context).unRegisterGoal();
}
}
}
Unregister the goal
It is a good practice to unregister the goal when the application is destroyed by the OS. To do this, add an additional boolean parameter which holds information about the registration status. This parameter should be set to "true" at the time of registration and to "false" at the time of goal deregistration.
Add a function to unregister a goal which is triggered at the time of destroying the application:
public class GoalEventClient {
//...
public void unRegisterGoal() {
if(!goalRegistered)
return;
goalRegistered = false;
mPassiveMonitoringClient.unregisterPassiveGoalCallback(mPassiveGoal);
mPassiveGoal = null;
}
//...
}
Test the application on a device
The application can be tested on the Galaxy Watch with Wear OS Powered by Samsung by running it directly from Android Studio. To test the application, follow these steps:
- Set the number of steps you want to achieve.
- Push the 'Set Goal' button.
3. Put your watch on your wrist and walk the set number of steps.
4. After reaching the goal, you receive information on the screen about achieving the goal and the watch vibrates.
Enjoy your adventure creating the ultimate health application
Now you are ready to start handling fitness goals using Health Services data in the background in your application on Galaxy Watch with Wear OS Powered by Samsung. Check out our next series of posts that cover ways to access exercises using the Health Services API!