gpt4 book ai didi

cordova - 使用 phonegap 检查设备的互联网连接

转载 作者:行者123 更新时间:2023-12-04 14:46:48 24 4
gpt4 key购买 nike

尝试使用cordova 2.0.0并使用此代码检查互联网连接

document.addEventListener("deviceready", onDeviceReady(), false);

function onDeviceReady() {
alert("ready");
db = window.openDatabase("loginintro", "1.0", "loginintro", 1000000);
db.transaction(populateDB, errorCB, successCB);
checkConnection();
}
function checkConnection()
{ alert("connection");
network = navigator.network.connection.type;
alert("fdfd");
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]);
}

但在这条线上出错

network = navigator.network.connection.type;

错误是:
04-09 15:20:23.989: E/Web Console(13329): Uncaught TypeError: Cannot read property 'connection' of undefined at file:///android_asset/www/js/lib/helloworldjs/alldatabse.js:14
04-09 15:20:39.419: E/CordovaWebView(13329): CordovaWebView: TIMEOUT ERROR!
04-09 15:20:39.419: D/Cordova(13329): CordovaWebViewClient.onReceivedError: Error code=-6 Description=The connection to the server was unsuccessful. URL=file:///android_asset/www/index.html

最佳答案

你在代码中有小错误。它应该是 alert('Connection type: ' + states[network]);

此外,请确保您拥有以下权限:

安卓

应用程序/res/xml/plugins.xml

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

应用程序/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

关于cordova - 使用 phonegap 检查设备的互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15894201/

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