EventListener "online" "offline" does not work in Android Cordova WebView

I have a little problem with Android Cordova Webview. My application does not start the eventListener "online / offline".

I try many types of listeners:

window.addEventListener('online', function() {alert('ON LISTENER');}); document.addEventListener('online', function() {alert('ON LISTENER');}); $(window).on("online", function() {alert('ON LISTENER');}); 

but no one works in android web browser.

+1
source share
1 answer

Here, “onLine” means connecting to the “network”, not to the “Internet”.

Tested using 5th generation Amazon Fire - Android 5.1.1 lollipop, Nook HD - Android 4.0.3 Ice cream sandwich and Samsung Galaxy S3 Neo - Android 4.4.2 KitKat

Found standard events and browser properties are unreliable, but it works reliably;

cordova network information plugin

eg. after deviceReady you can name it:

 function isOnLine(){ return navigator.connection.type !== Connection.NONE ; } 

or register them;

 window.addEventListener("online",someFunction); window.addEventListener("offline",someFunction); 

everything works as expected, except, it is possible that when launched, the (correct) online event / off is triggered.


In comparison, what I found using standard events and browser properties ...

mainly using the 5th generation Amazon Fire - Android 5.1.1 lollipop;

navigator.onLine starts as false, which may be incorrect (and read-only).

If at startup the device was connected and .. the device is disconnected - navigator.onLine = false .... the device is subsequently connected - navigator.onLine = true.

If at startup the device was disconnected and ...
the device is subsequently connected - navigator.onLine = true .... the device is disconnected - navigator.onLine = false.

After that, navigator.onLine displays changes in device connection status .... sometimes.

So, it starts as false (which may be incorrect) and then installs (unsatisfactorily) the next time the device state changes.

 window.addEventListener("online",online.bind(this)); 

and

 window.addEventListener("offline",offline.bind(this)); 

never starts (see also in Nook).

 window.ononline = function(){console.log("online");} 

and

 window.onoffline = function(){console.log("offline");} 

both launched and unfeasible, and depending on where Wi-Fi was connected in the device ... so the plugin seems to be now the only solution for Cordoba apps.

0
source

All Articles