Phonegap网络连接 - 无法读取未定义的属性“类型”

时间:2014-09-04 18:21:15

标签: android ios cordova phonegap-plugins

我一直在寻找答案,Google-sphere没有提供任何答案。我已经尝试了所有我知道如何做的事情,并通过建议的解决方案和答案进行了工作,没有任何效果。

简而言之,我正在尝试为PhonegapAndroid移动设备开发Apple应用,我需要的其中一项功能是检测网络状态和类型网络连接。以下是我正在使用的代码。

Firing device ready警报触发,然后我收到错误Cannot read property 'type' of undefined,然后循环遍历Navigator对象。当我浏览对象的每个属性时,我看不到connection属性,甚至没有看到旧版本中使用的network属性。

有人有什么想法吗?

的index.html

<!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<!-- jQuery Core -->
<script src="js/jquery-1.11.1.min.js"></script>
<!-- The main engine for the software. -->
<script src="js/main.js"></script>

<!-- Third party plugins -->
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="barcodescanner.js"></script>
<script type="text/javascript" src="childbrowser.js"></script>
<script type="text/javascript" src="js/barcode.js"></script>

<title>index</title>

<script>
document.addEventListener("deviceready", onDeviceReady, false);
</script>

</head>
<body>


<script>
barcode_app.initialize();
</script>

</body>
</html>

main.js

function onDeviceReady(){
    alert('Firing device ready');
    try{
        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.NONE]     = 'No network connection';

        $("#system_popup").html('Connection type: ' + states[networkState]);
        $("#system_popup").popup("open")
        //alert('Connection type: ' + states[networkState]);
    }catch(e){
        alert(e);
        $.each(navigator, function(key, value){
            alert(key+' => '+value);
        });
    }
}

在我的config.xml中,我有:

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

<gap:config-file platform="android" parent="/manifest">
        <uses-permission name="android.permission.ACCESS_NETWORK_STATE" />
    </gap:config-file>

    <gap:config-file platform="android" parent="/manifest">
        <uses-permission name="android.permission.INTERNET" />
    </gap:config-file>

    <gap:config-file platform="android" parent="/manifest">
        <uses-permission name="android.permission.READ_PHONE_STATE" />
    </gap:config-file>

<feature name="http://api.phonegap.com/1.0/device" />
    <feature name="NetworkStatus">
        <param name="android-package" value="org.apache.cordova.NetworkManager" />
        <param name="ios-package" value="CDVConnection" />
    </feature>





<小时/>



更新:解决方案

通过@Dawson Loudon和@benka的共同努力,最终制定了一个解决方案。道森纠正了我正在使用的插件本应该是:

<gap:plugin
        name="org.apache.cordova.network-information"
        version="0.2.7" />

这实际上只有在实现@benka提到的短暂延迟后才能正常工作。所以现在工作代码在JavaScript中看起来像这样:

function onDeviceReady(){
    try{
        var networkState = navigator.connection && navigator.connection.type;

        setTimeout(function(){
            networkState = navigator.connection && 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.NONE]     = 'No network connection';

            alert('Connection type: ' + states[networkState]);
        }, 500);
    }catch(e){
        alert(e);
        $.each(navigator, function(key, value){
            alert(key+' => '+value);
        });
    }
}

4 个答案:

答案 0 :(得分:6)

您想使用此处的插件:http://build.phonegap.com/plugins/626

<gap:plugin name="org.apache.cordova.network-information" version="0.2.7" />

答案 1 :(得分:2)

我已经在这里发布了这个想法:https://stackoverflow.com/a/19319817/2390075

我注意到networkState并不总是立即初始化。 那么对我有用的是在首先检查状态然后再次检查之后添加一个小延迟,在你的情况下它应该是这样的:

var networkState = navigator.connection && navigator.connection.type;

setTimeout(function(){
    networkState = navigator.connection && 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.NONE]     = 'No network connection';

    $("#system_popup").html('Connection type: ' + states[networkState]);
    $("#system_popup").popup("open")
    //alert('Connection type: ' + states[networkState]);
}, 500);

答案 2 :(得分:1)

迟到的回答,但这对我来说非常合适:

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

// device APIs are available
function onDeviceReady() {
    if(navigator.network.connection.type == Connection.NONE){
        alert("nocon");
    }else{
        alert("yescon");
    }
}

答案 3 :(得分:1)

意识到这是一个非常老的线程,但是我最近(11/2019)在尝试在Android上获取network.connection.type时遇到Cordova Plugin Network Information抛出未定义的问题(在iOS上似乎还不错)。我尝试了上述一些解决方案,但没有任何帮助。最终,我通过调用以下命令使其在Android上正常工作:

navigator.connection['type']

代替

navigator.connection.type

不知道为什么,但是现在可以了。