javascript navigator.network.connection.type 返回 null 而不是 Connection.NONE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8970197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:10:27  来源:igfitidea点击:

navigator.network.connection.type returns null instead of Connection.NONE

javascriptandroidhtmlcordovaconnection

提问by Jelmer

For some reason the Phonegap connection API returns null

由于某种原因,Phonegap 连接 API 返回 null

function checkConnection() {
    var networkState = navigator.network.connection.type;
    alert(networkState);
    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';

    document.getElementById('connections').innerHTML = states[networkState];
}

I run this function from the onDeviceReadyfunction which is called when the device is running.

我从onDeviceReady设备运行时调用的函数运行此函数。

When everything is turned off I get null. Shouldn't I get the Connection.NONEback instead of a null? It's really annoying since I can't check whether the phone has connection or not. It works fine when Wifi is turned on though.

当一切都关闭时,我得到null. 我不应该得到Connection.NONEback 而不是 anull吗?这真的很烦人,因为我无法检查手机是否已连接。不过,当 Wifi 打开时它工作正常。

回答by Werring

Make sure you have added

确保您已添加

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in AndroidManifest.xml

在 AndroidManifest.xml 中

回答by Radek Skokan

Have a look at the PhoneGap discussion group: https://groups.google.com/forum/?fromgroups#!searchin/phonegap/navigator.network.connection.type/phonegap/oAggxryQzrw/hE8uhwN3ONgJ

看看PhoneGap讨论组:https://groups.google.com/forum/?fromgroups#!searchin/phonegap/navigator.network.connection.type/phonegap/oAggxryQzrw/hE8uhwN3ONgJ

If you're using 1.6.0, you need to update res/xml/plugins.xml from

如果您使用的是 1.6.0,则需要从以下位置更新 res/xml/plugins.xml

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

to be:

成为:

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

(You need to remove the space in "Network Status")

(您需要删除“网络状态”中的空格)

回答by Kunal Nakashe

For the cordova version prior to 1.6 you need to use:

对于 1.6 之前的cordova 版本,您需要使用:

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

Instead of:

代替:

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

Please change the plugin name to "Network Status" with space!

请将插件名称更改为带有空格的“网络状态”!

回答by Gurnard

There is another change when going from pre 2.3.0 to post 2.3.0.

从 2.3.0 之前到 2.3.0 之后还有另一个变化。

Before Cordova 2.3.0, the Connection object existed at: navigator.network.connection.

To match the spec, this was changed to navigator.connection in 2.3.0.

navigator.network.connection still exists, but is now deprecated and will be removed in a future release.

在 Cordova 2.3.0 之前,Connection 对象存在于:navigator.network.connection。

为了符合规范,这在 2.3.0 中更改为 navigator.connection。

navigator.network.connection 仍然存在,但现在已弃用,并将在未来版本中删除。

2.7.0 docs

2.7.0 文档

So change this

所以改变这个

 var networkState = navigator.network.connection.type;

To This

至此

 var networkState = navigator.connection.type;

Basically remove the network from your Javascript. Hope this helps.

基本上从您的 Javascript 中删除网络。希望这可以帮助。

回答by rickyguff

I'm currently using PhoneGap 1.4.1 and Sencha Touch 1.1.1. on Android 2.3.5 (tested with HTC Desire S). It seems that the "navigator.network.connection.type" statement does effectively return 'null' with Phonegap 1.4.1, even if called after 'deviceready' event has been fired (confirmed after checking the source code). However it perfectly works with Cordova (ex-PhoneGap) 1.5.0. So I suggest to upgrade asap :-)

我目前使用 PhoneGap 1.4.1 和 Sencha Touch 1.1.1。在 Android 2.3.5 上(使用 HTC Desire S 测试)。似乎“navigator.network.connection.type”语句在Phonegap 1.4.1中确实有效地返回了'null',即使在'deviceready'事件被触发后调用(在检查源代码后确认)。但是,它与 Cordova(前 PhoneGap)1.5.0 完美配合。所以我建议尽快升级:-)

Eric.

埃里克。

回答by Karthik

Use this code:

使用此代码:

var networkState = navigator.connection.type;

Instead of:

代替:

var networkState = navigator.network.connection.type;