无法读取属性'连接'未定义的。在Cordova 3.2.0

时间:2014-09-02 06:31:03

标签: android cordova

我无法检查连接状态。我成功添加了插件org.apache.facebook.networkinformation。但我仍然有错误。

<script type="text/javascript" src="cordova.js"></script>
<script src="js/Connection.js"></script>
<script src="js/network.js"></script>
 <script type="text/javascript" charset="utf-8">

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


     function onDeviceReady() {
         alert(navigator.network.connection.type);
         alert(1);
     }


     function checkConnection() {

         alert(navigator.network.connection.type);
         var networkConnectionType = 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';
         alert('Connection Type: ' + states[networkConnectionType]);
     }

</script>

1 个答案:

答案 0 :(得分:1)

我刚刚检查了你的代码,我觉得它已经坏了。试试这个脚本告诉我,它是否适合你!

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]);
}

checkConnection();

在您的HTML代码中,您可以添加一个像 - &gt;

这样的按钮
<input type="button" onclick="checkConnection();">

因用户评论而被编辑

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


 function onDeviceReady {
    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]);
}