Triggers for background tasks
SensorCore SDK provides a set of triggers for background tasks:
StepCounterUpdate
- Raised when the user has moved at least 5 steps and at least 10 minutes have elapsed since the last step counter trigger to allow the app to update the step count in the live tile. This trigger is also raised at midnight for step counter applications to reset their daily statistics.
- Raised when the user has moved at least 5 steps and at least 10 minutes have elapsed since the last step counter trigger to allow the app to update the step count in the live tile. This trigger is also raised at midnight for step counter applications to reset their daily statistics.
MotionDataCleared
- Raised when motion data is cleared from settings. In Motion data settings 1.x, Motion data was cleared by tapping the clear motion data button. In motion data settings 2.x, Places and Tracks motion data is cleared automatically when the user turns off the Places visited setting. Steps and Activities motion data cannot be cleared.
- Raised when motion data is cleared from settings. In Motion data settings 1.x, Motion data was cleared by tapping the clear motion data button. In motion data settings 2.x, Places and Tracks motion data is cleared automatically when the user turns off the Places visited setting. Steps and Activities motion data cannot be cleared.
SenseApiStateChanged
- Raised when motion data or location has been switched on or off. When using motion data 1.x settings, this trigger is raised when the user toggles system location or motion data settings. When using motion data 2.x settings, this trigger is raised when the user toggles system location or Places visited settings.
Here, we explain how to add support for the StepCounterUpdate trigger in your application. Start by adding a new Windows Runtime Component project to your solution. If you already have a Windows Runtime Component in your solution, you can also use that.
Next you'll need to add a class to the runtime component that implements the IBackgroundTask interface:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.ApplicationModel.Background; namespace BackgroundTasks { /// <summary> /// Background task class for step counter update trigger /// </summary> public sealed class StepCounterTriggerTask : IBackgroundTask { /// <summary> /// Performs the work of a background task. The system calls this method when /// the associated background task has been triggered. /// </summary> /// <param name="taskInstance">An interface to an instance of the background task</param> public void Run( IBackgroundTaskInstance taskInstance ) { BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); // // Do something here // deferral.Complete(); } } }
Then, add a reference to the runtime component in your application.

Next you'll need to register the background task in your application. Notice that not all devices support all triggers, so you should call SenseHelper::GetSupportedApiSetAsync() to check whether a trigger is supported by the device.
public MainPage() { InitializeComponent(); this.Loaded += async( oo, ee ) => { if( await SenseHelper.GetSupportedApiSetAsync() >= 2 ) { await RegisterBackgroundTask( SenseTrigger.StepCounterUpdate, "stepcountertrigger", "BackgroundTasks.StepCounterTriggerTask" ); } }; } private async static Task RegisterBackgroundTask( String triggerName, String taskName, String taskEntryPoint ) { BackgroundAccessStatus result = await BackgroundExecutionManager.RequestAccessAsync(); if( result != BackgroundAccessStatus.Denied ) { // Remove the previous registration. foreach( var task in BackgroundTaskRegistration.AllTasks ) { if( task.Value.Name == taskName ) { task.Value.Unregister( true ); } } // Register the new trigger. BackgroundTaskBuilder myTaskBuilder = new BackgroundTaskBuilder(); var myTrigger = new DeviceManufacturerNotificationTrigger( triggerName, false ); myTaskBuilder.SetTrigger( myTrigger ); myTaskBuilder.TaskEntryPoint = taskEntryPoint; myTaskBuilder.Name = taskName; BackgroundTaskRegistration myTask = myTaskBuilder.Register(); } }
Finally, you'll need to declare the background task in the application manifest. Add the following to Package.appxmanifest:
<Extensions> <Extension Category="windows.backgroundTasks" EntryPoint="BackgroundTasks.StepCounterTriggerTask"> <BackgroundTasks> <Task Type="systemEvent" /> </BackgroundTasks> </Extension> </Extensions>
Now, build and deploy your application. The background task should get triggered when the user has moved at least 5 steps and at least 10 minutes have elapsed since the last step counter trigger.
As opposed to normal system triggers, SensorCore SDK triggers do not enforce CPU usage limits. Also, you will receive the triggers regardless of whether the device is running on battery power.
Please refer to
MSDN
for more information regarding triggers and background tasks.