This topic describes how to monitor network status changes and retrieve the current network status.
Most Web applications use a network connection to, for example, play media and download files. Because the network connection can disconnect and reconnect unexpectedly, checking the network status regularly is very important for application stability.
You can use the Samsung Product Network API to monitor network status changes and retrieve the current network status.
If your application content cannot be accessed due to HTTP client or server issues, you must show a clear error message with information on what to do or who to contact for support. Remember that Samsung cannot provide support to TV users with issues related to content availability within your application.
To use the Network API, the application has to request permission by adding the following privilege to the "config.xml" file:
<tizen:privilege name='http://developer.samsung.com/privilege/network.public'> </tizen:privilege>
To receive notifications when the network status changes, add the addNetworkStateChangeListener() method to your window.onload event:
addNetworkStateChangeListener()
window.onload
webapis.network.addNetworkStateChangeListener(function(value) { if (value == webapis.network.NetworkState.GATEWAY_DISCONNECTED) { // Something you want to do when network is disconnected } else if (value == webapis.network.NetworkState.GATEWAY_CONNECTED) { // Something you want to do when network is connected again } });
The callback returns NetworkState.GATEWAY_DISCONNECTED when the network has become disconnected. You must stop jobs requiring a network connection, such as network media playback and server request sending. Return the user to the previous page and inform them of the disconnected status using a popup.
NetworkState.GATEWAY_DISCONNECTED
The callback returns NetworkState.GATEWAY_CONNECTED when the network has reconnected.
NetworkState.GATEWAY_CONNECTED
When resuming media playback, or resuming an application from the pause state, check the current network status. You can retrieve the current network status in 2 ways:
Using the getActiveConnectionType() method:
getActiveConnectionType()
var activeConnectionType = webapis.network.getActiveConnectionType(); if (activeConnectionType == 0) { // Something you want to do when network is disconnected }
Using the isConnectedToGateway() method:
isConnectedToGateway()
var gatewayStatus = webapis.network.isConnectedToGateway(); if (!gatewayStatus) { // Something you want to do when network is disconnected }