23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Note Windows Store apps written in JavaScript can also use the basic window.nagivator.ononline<br />

and window.navigator.onoffline events to track connectivity. The window.navigator.onLine<br />

property is also true or false accordingly. These events, however, will not alert you to changes in<br />

connection profiles, cost, or other aspects that aren’t related to the basic availability of an Internet<br />

connection.<br />

You can play with networkstatuschanged in Scenario 5 of the Network information sample. As you<br />

connect and disconnect networks or make other changes, the sample will update its details output for<br />

the current Internet profile if one is available (code condensed from js/network-status-change.js):<br />

var networkInfo = Windows.Networking.Connectivity.NetworkInformation;<br />

// Remember to removeEventListener for this event from WinRT as needed<br />

networkInfo.addEventListener("networkstatuschanged", onNetworkStatusChange);<br />

function onNetworkStatusChange(sender) {<br />

internetProfileInfo = "Network Status Changed: \n\r";<br />

var internetProfile = networkInfo.getInternetConnectionProfile();<br />

if (internetProfile === null) {<br />

// Error message<br />

} else {<br />

internetProfileInfo += getConnectionProfileInfo(internetProfile) + "\n\r";<br />

// display info<br />

}<br />

}<br />

internetProfileInfo = "";<br />

Of course, listening for this event is useful only if the app is actually running, but what if it isn’t? In<br />

that case an app needs to register a background task, as discussed at the end of Chapter 13, for the<br />

networkStateChange trigger, typically applying the internetAvailable or internetNot-Available<br />

conditions as needed. The Network status background sample provides a demonstration of this,<br />

declaring a background task in its manifest with a C# entry point of NetworkStatusTask.-<br />

NetworkStatusBackgroundTask. The task is registered in js/network-status-with-internet-present.js (using<br />

helpers in js/global.js as typical for the background task samples):<br />

BackgroundTaskSample.registerBackgroundTask(BackgroundTaskSample.sampleBackgroundTaskEntryPoint,<br />

BackgroundTaskSample.sampleBackgroundTaskWithConditionName,<br />

new Windows.ApplicationModel.Background.SystemTrigger(<br />

Windows.ApplicationModel.Background.SystemTriggerType.networkStateChange, false),<br />

new Windows.ApplicationModel.Background.SystemCondition(<br />

Windows.ApplicationModel.Background.SystemConditionType.internetAvailable));<br />

The background task in BackgroundTask.cs simply writes the Internet profile name and network<br />

adapter id to local app data in response to the trigger. These values are output to the display within the<br />

completeHandler in js/global.js. A real app would clearly take more meaningful action, such as activating<br />

background transfers for data synchronization when connectivity is restored. The basic structure is there<br />

in the sample nonetheless.<br />

635

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

Saved successfully!

Ooh no, something went wrong!