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.
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:
...
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With