我正在我的phonegap应用中运行这段代码。 代码执行完全没有错误,但有一个问题, 无论何时它总是显示UNKNOWN CONNECTION,我在设备或模拟器上运行应用程序,为什么会出现网络类型始终未知的原因。
function checkConnection() {
var networkState = navigator.connection.type;
var states = {};
states[navigator.connection.UNKNOWN] = 'Unknown Connection';
states[navigator.connection.ETHERNET] = 'Ethernet connection';
states[navigator.connection.WIFI] = 'WiFi connection';
states[navigator.connection.CELL_2G] = 'Cell 2G connection';
states[navigator.connection.CELL_3G] = 'Cell 3G connection';
states[navigator.connection.CELL_4G] = 'Cell 4G connection';
states[navigator.connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
checkConnection();
答案 0 :(得分:0)
你的代码没有错误。你在onDeviceReady()上调用了checkConnection()方法。另外请检查link 将您的网络状态设置为;
var networkState = navigator.network.connection.type;
请试一试。
function checkConnection() {
var networkState = 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[networkState]);
}
checkConnection();
另外请检查您是否添加了不同平台所需的权限。
答案 1 :(得分:0)
我在“config.xml”中更改了功能配置时解决了同样的问题。如果您正在尝试使用Android设备,请
<feature name="NetworkStatus">
<param name="android-package" value="org.apache.cordova.NetworkManager" />
</feature>
尝试
<feature name="NetworkStatus">
<param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" />
</feature>
(在“NetworkManager”之前添加“ networkinformation。”)
答案 2 :(得分:0)
This comment适合我。
在cordova-plugin的文档中,您必须确保查看正确版本的cordova。 示例:对于Cordova 2.5.0
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />
<feature name="NetworkStatus">
<param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" />
</feature>