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

发布于 2024-12-28 13:44:00 字数 877 浏览 1 评论 0原文

由于某种原因,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];
}

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

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

For some reason the Phonegap connection API returns 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 onDeviceReady function which is called when the device is running.

When everything is turned off I get null. Shouldn't I get the Connection.NONE back 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

对岸观火 2025-01-04 13:44:00

确保您已

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

在 AndroidManifest.xml 中添加

Make sure you have added

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

in AndroidManifest.xml

甜柠檬 2025-01-04 13:44:00

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

如果您使用的是 1.6。 0,需要将res/xml/plugins.xml更新为

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

:(

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

需要去掉“网络状态”中的空格)

Have a look at the PhoneGap discussion group: 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

<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")

情泪▽动烟 2025-01-04 13:44:00

我目前正在使用 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。

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 :-)

Eric.

夏の忆 2025-01-04 13:44:00

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

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

而不是:

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

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

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

<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!

橘虞初梦 2025-01-04 13:44:00

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

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

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

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

2.7.0 docs

因此将其更改

 var networkState = navigator.network.connection.type;

为此

 var networkState = navigator.connection.type;

基本上删除网络从你的 JavaScript 中。希望这有帮助。

There is another change when going from pre 2.3.0 to post 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.

2.7.0 docs

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.

栀梦 2025-01-04 13:44:00

使用此代码:

var networkState = navigator.connection.type;

而不是:

var networkState = navigator.network.connection.type;

Use this code:

var networkState = navigator.connection.type;

Instead of:

var networkState = navigator.network.connection.type;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文