嘿我想在cordova上检查我的网络状态。当我的状态是 4g,3g,没有网络时,我的应用会提供正确的警报,但是在wifi上它表示连接未知。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="format-detection" content="telephone-no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, minimum-scale=1, maximum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi">
<title>Our Application</title>
<link rel="stylesheet" href="css/themes/default/jquery.mobile-1.3.2.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/jquery.js"></script>
<script src="js/jquery.mobile-1.3.2.js"></script>
<script src="js/mustache.js"></script>
<script src="cordova.js"></script>
<script>
function init() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
checkConnection();
}
function checkConnection() {
var networkState = navigator.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.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
</script>
</head>
<body onload="init()">
<div data-role="page" id="main">
<div data-role="header" class="logo">
<img src="img/logo.png" />
</div>
</div>
</body>
</html>
有趣的是,我已经尝试了关于API文档的完整示例(即使我的代码与示例几乎相同)
http://cordova.apache.org/docs/en/3.1.0/cordova_connection_connection.md.html#Connection
这里也发生了同样的错误。 以防我分享我的设备信息。
我的手机:Sony Xperia Z. 型号:C6603 Android版本:4.2.2 编号:10.31.A.2.67