gpt4 book ai didi

networking - navigator.connection.type 即使设备已准备就绪 * 或 * 设备从未准备好也无法正常工作

转载 作者:行者123 更新时间:2023-12-04 01:59:46 26 4
gpt4 key购买 nike

我正在尝试使用 Phonegap 制作一个简单的应用程序,该应用程序是使用 Adob​​e Phonegap 构建器编译的。我已经找到并使用了下面的使用 navigator.connection.type 的有据可查的示例,并且我在其中添加了另一行,以便在设备准备就绪时生成一个警告框。它甚至没有那么远。通过将此代码从页面的头部移动到页面的主体,我在某些时候显示了无尽的旋转圆圈,但这最终没有帮助。在 iOs 和 Android 设备上测试给出了相同的结果,并且 config.xml 确实包括:-

<plugin name="NetworkStatus" value="CDVConnection" />
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />

非常感谢任何帮助。

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is loaded and it is now safe to make calls Cordova methods
//
function onDeviceReady() {
alert('Device is ready');
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>

最佳答案

您还应该等到所有脚本都加载完毕。像这样将所有内容包装在 onBodyLoad 中:

function onBodyLoad() {
// these are useful later in the app, might as well set early
window.isRipple = (window.tinyHippos != null);
window.isPhoneGap = /^file:\/{3}[^\/]/i.test(window.location.href);
window.isIOS = !window.isRipple && navigator.userAgent.match(/(ios|iphone|ipod|ipad)/gi) != null;
window.isAndroid = !window.isRipple && navigator.userAgent.match(/(android)/gi) != null;

// stuff I use for debugging in chrome
if (window.isPhoneGap) {
document.addEventListener("deviceready", onDeviceReady, false);
} else {
onDeviceReady();
}
}

并添加到您的正文标签中:

<body onload="onBodyLoad()"> 

以及我的其余代码以供引用:

function checkOffLine(minutes) {
if (window.lastCheckTime == null) {
window.lastCheckTime = 0;
}

var currentTime = (new Date()).getTime();
if (currentTime < (window.lastCheckTime + minutes * 60000)) return;
window.lastCheckTime = currentTime;

// ios does not allow you to exit the application so just warn
// ios also require you to warn or your app get rejected
if (window.isIOS) {
navigator.notification.alert('This application may not function properly without an internet connection.');
} else {
navigator.notification.confirm(
'This application may not function properly without an internet connection. Continue working offline?', // message
function(button) // callback to invoke with index of button pressed
{
if (button == 1) {
navigator.app.exitApp();
}
},
'Warning', // title
'Exit,Continue' // buttonLabels
);
}
}

function checkConnection() {
// your check connection type code here or just use navigator.onLine
if (!navigator.onLine) {
// don't be annoying, only confirm for once every every 5 minutes
checkOffLine(5);
}
}

// initial phonegap deviceready handler
function onDeviceReady() {
console.log('Application started');
angular.bootstrap(document, ['assetsApp']);
if (window.isPhoneGap) {
document.addEventListener("offline", checkConnection, false);
checkConnection();
}
};

function onBodyLoad() {
// these are useful later in the app, might as well set early
window.isRipple = (window.tinyHippos != null);
window.isPhoneGap = /^file:\/{3}[^\/]/i.test(window.location.href);
window.isIOS = !window.isRipple && navigator.userAgent.match(/(ios|iphone|ipod|ipad)/gi) != null;
window.isAndroid = !window.isRipple && navigator.userAgent.match(/(android)/gi) != null;

// stuff I use for debugging in chrome
if (window.isPhoneGap) {
document.addEventListener("deviceready", onDeviceReady, false);
} else {
onDeviceReady();
}
}

关于networking - navigator.connection.type 即使设备已准备就绪 * 或 * 设备从未准备好也无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17750667/

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