Skip to content Skip to sidebar Skip to footer

EventListener "online" "offline" Doesn't Work In Android Cordova WebView

I have a little problem with Android Cordova Webview. My app doesn't trigger the eventListener 'online/offline'. I try many types of listeners: window.addEventListener('online', fu

Solution 1:

Here "onLine" means connected to "a network" as opposed to "the internet".

Tested using 5th gen 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 the standard browser events and properties unreliable but this seems to work reliably ;

cordova network information plugin

eg. after deviceReady you can call this ;

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

or register these ;

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

all work as expected except perhaps that the (correct) online/offline event fires on startup.


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

mostly using a 5th gen Amazon Fire - Android 5.1.1 lollipop;

navigator.onLine starts out as false which may be incorrect ( and is read only ).

If, when started, device was connected and.. device subsequently disconnected - navigator.onLine = false.... device subsequently connected - navigator.onLine = true.

If, when started, device was disconnected and...
device subsequently connected - navigator.onLine = true.... device subsequently disconnected - navigator.onLine = false.

Thereafter navigator.onLine reflects device connection status changes....sometimes.

So, it starts as false (which may be incorrect) and is then set (unreliably) when the device state next changes.

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

and

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

never fired ( seen on Nook also ).

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

and

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

both fired but unreliably and depending on where in the device the wifi was toggled... so the plugin appears to be the only solution for Cordova apps for now.


Post a Comment for "EventListener "online" "offline" Doesn't Work In Android Cordova WebView"