我navigator.network.connection.type
用来获取设备的网络状态。但它显示了错误
TypeError: Result of expression 'navigator.network' [undefined] is not an object.
我尝试使用 Phonegap 1.0.0、Cordova 1.7.0、Cordova 1.7.0rc1,但我仍然遇到同样的错误。
它在 iOS 上运行良好,但在 Android 上却不行。有人可以帮我弄这个吗?
这是我的代码:
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady() {
checkConnection();
loaddb();
}
function checkConnection() {
alert(navigator.network);
if(navigator.network==undefined) {
window.localStorage.setItem("internetAccessFlag","false");
} else {
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';
if(networkState==Connection.UNKNOWN || networkState==Connection.NONE) {
window.localStorage.setItem("internetAccessFlag","false");
} else {
window.localStorage.setItem("internetAccessFlag","true");
}
//alert(window.localStorage.getItem("internetAccessFlag"));
}
}
</script>
编辑:-我正在使用 Cordova 1.7.0 和 plugins.xml
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
我和这有什么关系吗?