EventListener“在线” “离线”在Android Cordova WebView中不工作 [英] EventListener "online" "offline" doesn't work in Android Cordova WebView

查看:278
本文介绍了EventListener“在线” “离线”在Android Cordova WebView中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Android Cordova Webview有一点问题。我的应用程序不会触发eventListener在线/离线。



我尝试很多类型的侦听器:

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

但在android webview中无效

解决方案

这里onLine表示连接到网络而不是互联网。



使用
测试第5代Amazon Fire - Android 5.1.1 lollipop,
Nook HD - Android 4.0.3冰淇淋三明治和
三星Galaxy S3 Neo - Android 4.4.2 KitKat



找到标准的浏览器事件和属性不可靠,但这似乎可靠地工作;



cordova网络信息插件



例如。 after deviceReady您可以调用此方法;

 函数isOnLine(){
return navigator.connection.type!== Connection 。没有 ;
}

或注册这些;

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

都可以正常工作,除非(正确的)在线/离线事件在启动时触发。 p>




相比之下,我使用标准的浏览器事件和属性...



主要使用第5代Amazon Fire - Android 5.1.1 lollipop;



navigator.onLine



如果启动时,设备已连接,...
设备随后断开连接 - navigator.onLine = false ....
设备后续连接 - navigator.onLine = true。



如果启动时设备已断开连接,并且...

设备随后连接 - navigator.onLine = true ....
设备后来断开连接 - navigator.onLine = false。



< navigator.onLine 反映设备连接状态更改....有时。



因此,它开始为false(可能不正确),然后在设备状态下一次更改时设置(不可靠)。

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

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

从未触发(在Nook也可见)。

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

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

两者都被触发,但不可靠,并且取决于设备在哪里切换wifi ...该插件似乎是目前Cordova应用程序的唯一解决方案。


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', function() {alert('ON LISTENER');});
document.addEventListener('online', function() {alert('ON LISTENER');});
$(window).on("online", function() {alert('ON LISTENER');});

but none work in android webview

解决方案

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.

这篇关于EventListener“在线” “离线”在Android Cordova WebView中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆