navigator.network.connection.type 返回 null 而不是 Connection.NONE
由于某种原因,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
确保您已
在 AndroidManifest.xml 中添加
Make sure you have added
in AndroidManifest.xml
查看 PhoneGap 讨论组:https://groups.google.com/forum/?fromgroups#!searchin/phonegap/navigator.network.connection.type/phonegap/oAggxryQzrw/hE8uhwN3ONgJ
如果您使用的是 1.6。 0,需要将res/xml/plugins.xml更新为
:(
需要去掉“网络状态”中的空格)
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
to be:
(You need to remove the space in "Network Status")
我目前正在使用 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.
对于 1.6 之前的 cordova 版本,您需要使用:
而不是:
请将插件名称更改为带有空格的“网络状态”!
For the cordova version prior to 1.6 you need to use:
Instead of:
Please change the plugin name to "Network Status" with space!
从 2.3.0 之前到 2.3.0 之后还有另一个变化。
2.7.0 docs
因此将其更改
为此
基本上删除网络从你的 JavaScript 中。希望这有帮助。
There is another change when going from pre 2.3.0 to post 2.3.0.
2.7.0 docs
So change this
To This
Basically remove the network from your Javascript. Hope this helps.
使用此代码:
而不是:
Use this code:
Instead of: