离线和在线活动未在cordova 3.5.0中调用

我正在使用cordova 3.5.0来开发我的phonegap应用程序。 因为我想在网络服务电话之前检查互联网连接。 所以我通过使用命令cordova plugin add org.apache.cordova.network-information添加了网络状态插件。 插件已在我的应用程序中成功安装。

添加插件后,我添加了2个EventListener,一个用于在线,另一个用于离线。

 var app = { // Application Constructor initialize: function() { console.log('App initializing...'); this.bindEvents(); }, // Bind Event Listeners // // Bind any events that are required on startup. Common events are: // 'load', 'deviceready', 'offline', and 'online'. bindEvents: function() { //This is to allow testing in desktop browser where there is no device ready event if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) { document.addEventListener('deviceready', this.onDeviceReady, false); document.addEventListener("offline", onOffline, false); document.addEventListener("online", onOnline, false); } else { this.onDeviceReady(); } }, // deviceready Event Handler // // The scope of 'this' is the event. In order to call the 'receivedEvent' // function, we must explicity call 'app.receivedEvent(...);' onDeviceReady: function() { StatusBar.overlaysWebView(false); app.receivedEvent('deviceready'); }, onOnline:function(){ console.log("Online"); }. onOffline: function(){ console.log("Offline"); }, // Update DOM on a Received Event receivedEvent: function(id) { require(['router'], function(Router){ Router.getInstance(); console.log('Backbone callback...'); }); } }; 

所以我使用另一种方法来检查Phonegap文档中提到的在线状态

 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]); } 

这总是返回连接到互联网的模式,例如“WiFi连接”。 虽然我改变了我的断开我的互联网连接。

帮我解决这个问题。

我有同样的问题:在线/离线事件没有解雇,有时当它下线时,应用程序崩溃了……

解决方案: – 在AndroidManifest.xml文件中,相应的权限标记必须在标记之后立即显示…它可能看起来很荒谬,但是否则事件未触发。

最后,您的文件将如下所示:

    

范围。

 document.addEventListener("offline", this.onOffline, false); document.addEventListener("online", this.onOnline, false); 

我避免使用网络类型,因为我发现它们不能在所有平台上一致地工作。

相反,我使用:

  isOnline = function() { try { if (!navigator.onLine){ return false; } else { return true; } } catch(e) { alert('An error has occurred: '+e.message); } }; 

我会做的事情。

  • 首先只在onDeviceReady函数中添加onlineoffline onDeviceReady
  • 其次,在事件侦听器中为onOfflineonOnline方法调用添加范围。

像这样的东西:

 if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) { document.addEventListener('deviceready', this.onDeviceReady, false); } else { this.onDeviceReady(); } }, onDeviceReady: function() { StatusBar.overlaysWebView(false); app.receivedEvent('deviceready'); document.addEventListener("offline", app.onOffline, false); document.addEventListener("online", app.onOnline, false); }, onOnline:function(){ console.log("Online"); }. onOffline: function(){ console.log("Offline"); },