Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Internet connectivity is available for multiple pages in Windows 8 Store HTML/JS app?

In the home page, I've used registerForNetworkStatusChangeNotif() function (which I borrowed from MSDN) for registering network notification change. The function adds an event listener networkstatuschanged and it works fine for that page alone i.e. an error is shown when connectivity is interrupted and the page is refreshed automatically when connectivity is restored -

var networkInfo = Windows.Networking.Connectivity.NetworkInformation; ... networkInfo.addEventListener("networkstatuschanged", onNetworkStatusChange);

How should I handle the Internet availability check for other pages?

I've tried registering this function in default.js so that it is available for all the pages. My app uses the Geolocation feature and the statuschanged event clashes with the networkstatuschanged event and my app fails to show the loss of Internet connectivity error. How can I resolve this issue so that unavailability of Internet connectivity is properly handled.

like image 454
mvp Avatar asked Nov 13 '12 17:11

mvp


1 Answers

You need to listen to a specific event. And when it fires, you check the internet connectivity.

var networkInformation = Windows.Networking.Connectivity.NetworkInformation;
...

ready: function(element, options)
{
   // Registering for connection change
   networkInformation.addEventListener("networkstatuschanged", this.onNetworkStatusChanged);
   ...
}

unload: function()
{
   // Unregistering for connection change
   networkInformation.removeEventListener("networkstatuschanged", this.onNetworkStatusChanged);
   ...
}

onNetworkStatusChanged: function(eventArgs)
{
    // Retrieve the InternetConnectionProfile
    var internetConnectionProfile = networkInformation.getInternetConnectionProfile();
    // Accesses the NetworkConnectivityLevel
    var networkConnectivityLevel = internetConnectionProfile.getNetworkConnectivityLevel();
    // Switch on NetworkConnectivityLevel
    switch (networkConnectivityLevel) {
        case Windows.Networking.Connectivity.NetworkConnectivityLevel.none:
           ...
        case Windows.Networking.Connectivity.NetworkConnectivityLevel.localAccess:
           ...
        case Windows.Networking.Connectivity.NetworkConnectivityLevel.constrainedInternetAccess:
           ...
        case Windows.Networking.Connectivity.NetworkConnectivityLevel.internetAccess:
           ...
    }
    ...
}
like image 155
Cœur Avatar answered Nov 15 '22 12:11

Cœur