Checking Network Status
This topic describes how to monitor network status changes and retrieve the current network status.
Related Info
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.
Prerequisites
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>
Monitoring Network Status Changes
To receive notifications when the network status changes, add the addNetworkStateChangeListener()
method to your window.onload
event:
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. -
The callback returns
NetworkState.GATEWAY_CONNECTED
when the network has reconnected.
Retrieving the Network Status
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:var activeConnectionType = webapis.network.getActiveConnectionType(); if (activeConnectionType == 0) { // Something you want to do when network is disconnected }
-
Using the
isConnectedToGateway()
method:var gatewayStatus = webapis.network.isConnectedToGateway(); if (!gatewayStatus) { // Something you want to do when network is disconnected }