I'm currently trying to check if my app user is or isn't connected to internet.
I've added the plugin Device,Network Information.
This works completely fine in the emulator but when I test in through WIFI on a iPad and a HTC phone the alert does not work.
Code I am using is below.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert(intel.xdk.device.connection);
}
链接已复制
Hi,
The api intel.xdk.device is retired, so is not advisable to use it. Here you have more information:
https://software.intel.com/en-us/xdk/docs/intel-xdk-api-cordova-plugin-methods-properties-events
One alternative is
https://github.com/apache/cordova-plugin-network-information
Regards,
Diego
Hi Diego,
I already have the plugin network-information.
I tried to use this code as listed below. However this now no longer works in the emulator or test.
checkConnection(); function checkConnection() { 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.CELL] = 'Cell generic connection'; states[Connection.NONE] = 'No network connection'; alert('Connection type: ' + states[networkState]); }
It is working form me, both in emulator and in App Preview in an iPhone.
In Emulator in my Mac, the alert says Ethernet connection.
In App Preview, gives wi-fi connection. If I turn off wi-fi, says No network connection (I expect 3G, but no).
What I did is, I an HTML5+Cordova project I added the Network Information plugin from Core Plugins, drop a button and add to the click event your code above.
The plugin is version 1.2.0 and set android permissions to android.permission.ACCESS_NETWORK_STATE
Regards,
Diego
