gpt4 book ai didi

android - Phonegap/Cordova - TypeError : Result of expression 'navigator.network' [undefined] is not an object

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:03 24 4
gpt4 key购买 nike

我正在使用 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"/>

这与我有什么关系吗?

最佳答案

这段代码非常适合我:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
<script type="text/javascript" charset="utf-8">

var deviceInfo = function(){
alert(navigator.network.connection.type);
}

function init(){
console.log("GOT AN ONLOAD!!!")
document.addEventListener("deviceready", deviceInfo, true);
}
</script>
</head>
<body onload="init();">
A body...
</body>
</html>

此外,当您从 iOS 迁移到 Android 时,您使用的是 Android 版本的 cordova.js 吗?每个平台都有一个依赖于平台的 cordova.js。

关于android - Phonegap/Cordova - TypeError : Result of expression 'navigator.network' [undefined] is not an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10864881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com