我正在使用 Phonegap。我需要定期检查网络连接。实际上我正在从服务器获取一些数据。如果没有连接,我需要显示错误警报。
我用谷歌搜索并找到了解决方案。但这不行。因为我需要定期检查连接。
<html>
<head>
<title>navigator.network.connection.type Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
checkConnection();
}
function checkConnection() {
var networkState = navigator.network.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
</script>
</head>
<body>
<p>A dialog box will report the network state.</p>
</body>
</html>
它仅在应用程序启动时第一次检查。但是我需要定期检查它,因为我正在做套接字编程。如果互联网有问题,我需要显示它。但此代码仅在启动时显示。