6

尝试使用 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
4

3 回答 3

4

您在代码中有小错误。应该是 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" />
于 2013-04-09T07:57:18.603 回答
3

API 已更改,您应该参考 navigator.connection。

network = navigator.connection.type;

然后,看本卡的回答

于 2014-01-10T10:43:59.293 回答
0

我在使用最新版本的科尔多瓦时遇到了这个问题。我在 config.xml 中有网络插件,但它没有下载到插件目录中。

这修复了它

科尔多瓦插件添加科尔多瓦插件网络信息科尔多瓦构建

存储库名称已更改,以防您习惯:

cordova 插件添加 org.apache.cordova.network-information

于 2016-03-23T00:26:09.953 回答