23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Note Working with the lock screen is not supported in the Visual Studio simulator. To debug lock<br />

screen apps and background tasks, you’ll need to use the Local Machine or Remote Machine<br />

debugging options.<br />

Background tasks for these triggers are created and registered as we’ve already seen. A TimeTrigger,<br />

for example, is created with its freshnessTime interval (in minutes) and a oneShot flag, as seen in<br />

Scenario 5 of the Background tasks sample (js/time-trigger-background-task.js):<br />

BackgroundTaskSample.registerBackgroundTask(<br />

BackgroundTaskSample.sampleBackgroundTaskEntryPoint,<br />

BackgroundTaskSample.timeTriggerTaskName,<br />

new Windows.ApplicationModel.Background.TimeTrigger(15, false), null);<br />

A TimeTrigger is also used in Scenario 3 of the Geolocation sample to allow the user to add a<br />

navigation app to the lock screen for more continuous tracking. Generally speaking, though, a<br />

navigation app isn’t particularly useful on the lock screen in the first place, since it wouldn’t be able to<br />

show a map! Better, then, to again use the Windows.System.Display.DisplayRequest API to prevent<br />

going to the lock screen at all.<br />

Creating a PushNotificationTrigger is even simpler as there are no parameters. This can be seen in<br />

the Raw notifications sample, Scenario 1 (js/scenario1.js):<br />

function registerBackgroundTask() {<br />

// Register the background task for raw notifications<br />

var taskBuilder = new background.BackgroundTaskBuilder();<br />

var trigger = new background.PushNotificationTrigger();<br />

taskBuilder.setTrigger(trigger);<br />

taskBuilder.taskEntryPoint = sampleTaskEntryPoint;<br />

taskBuilder.name = sampleTaskName;<br />

}<br />

var task = taskBuilder.register();<br />

task.addEventListener("completed", backgroundTaskComplete);<br />

Although the call to BackgroundTaskBuilder.register might succeed, the task itself will not execute<br />

until the user add the app to the lock screen, as we saw earlier in Figure 13-10. This latter action is never<br />

under the app’s control—all it can do is make sure it’s available for the user to select on that section of<br />

PC Settings, which is what asking for access is all about.<br />

The request is made through the method Windows.ApplicationModel.Background.-<br />

BackgroundExecutionManager.requestAccessAsync; this call should be made prior to registering the<br />

background task (see Scenario 5 of the Background task sample again):<br />

Windows.ApplicationModel.Background.BackgroundExecutionManager.requestAccessAsync();<br />

When this is called the first time in an app, it will generate a user consent prompt, as shown in Figure<br />

13-19. If the user chooses Allow, the app will appear in PC Settings as an option for the lock screen,<br />

otherwise it won’t. As with other permissions, users can change their minds later on through the<br />

Permissions settings, as shown in Figure 13-20.<br />

625

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!