How to use the maintenance trigger (HTML)
Learn how to use the MaintenanceTrigger class to run lightweight code in the background while the device is plugged in.
What you need to know
Technologies
Prerequisites
- This example assumes that you have lightweight code you can run in the background to enhance your app while the device is plugged in. This topic focuses on the MaintenanceTrigger class, which is similar to SystemTrigger in that the app is not required to be on the lock screen to register a MaintenanceTrigger background task. More information on writing a background task class is available in Quickstart: Create and register a background task.
Instructions
Step 1: Create a maintenance trigger object
Create a new MaintenanceTrigger object. The second parameter, OneShot, specifies whether the maintenance task will run once or continue to run periodically. If OneShot is set to true, the first parameter (FreshnessTime) specifies the number of minutes to wait before scheduling the background task. If OneShot is set to false, FreshnessTime specifies the frequency by which the background task will run.
This example code creates a trigger that runs once an hour:
var waitIntervalMinutes = 60; var taskTrigger = new Windows.ApplicationModel.Background.MaintenanceTrigger(waitIntervalMinutes, false);
Step 2: (Optional) Add a condition
If necessary, add a background task condition to control when the task runs. A condition prevents your background task from running until the condition is met - for more information, see How to set conditions for running a background task.
In this example, the condition is set to UserPresent so that maintenance runs when the user is active (or when the user becomes active). For a list of possible background task conditions, see SystemConditionType.
The following code adds a condition to the maintenance task builder:
var exampleCondition = new Windows.ApplicationModel.Background.SystemCondition(Windows.ApplicationModel.Background.SystemConditionType.UserAway);
Step 3: Register the background task
Register the background task by calling your background task registration function. For more information on registering background tasks, see How to register a background task.
The following code registers the background task:
var entryPoint = “js\\ExampleBackgroundTask.js”; var taskName = “Maintenance background task example”; var task = RegisterBackgroundTask(entryPoint, taskName, taskTrigger, exampleCondition);
Note
In Windows Phone Store apps, you must call RequestAccessAsync before attempting to register any background task. On Windows, this call is only required for the set of background tasks that require your app to be on the lock screen to run, but on the phone you must call this method once before registering any background task.
To ensure that your Windows Phone app continues to run properly after you release an update, you must call RemoveAccess and then call RequestAccessAsync when your app launches after being updated. For more information, see Guidelines for background tasks (HTML).
Starting in Windows 8.1, background task registration parameters are validated at the time of registration. An error is returned if any of the registration parameters are invalid. Your app must be able to handle scenarios where background task registration fails - for example, use a conditional statement to check for registration errors and then retry failed registration using different parameter values.
Remarks
Maintenance trigger events will not fire unless the device is plugged in.
Related topics
- Quickstart: Create and register a background task
- How to register a background task
- How to respond to system events with background tasks
- How to set conditions for running a background task
- How to declare background tasks in the application manifest
- How to debug a background task
- Guidelines and checklists for background tasks