科尔多瓦的在线和离线活动并不总是开火

时间:2016-12-30 11:51:20

标签: javascript android cordova

我正在使用Cordova编写一个简单的Android应用程序,并遇到网络信息插件的一些困难。解雇“离线”和“在线”事件对我来说很奇怪。

首先,我曾经在“onDeviceReady”函数中添加事件监听器,因为我在阅读的一些指南中建议使用它。

    var app = {
        // Application Constructor
        initialize: function() {
            this.bindEvents();
        },

        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        },

        onDeviceReady: function() {
            app.receivedEvent('deviceready');
            document.addEventListener("online", function(){alert('online : true') }, false);
            document.addEventListener("offline", function(){alert('online : true') }, false);

            // Here, we redirect to the web site.
            var targetUrl = "https://sputnik.geoscan.aero/";
            var bkpLink = document.getElementById("bkpLink");
            bkpLink.setAttribute("href", targetUrl);
            bkpLink.text = targetUrl;
            window.location.replace(targetUrl);
            },

        receivedEvent: function(id) {
            var parentElement = document.getElementById(id);
            var listeningElement = parentElement.querySelector('.listening');
            var receivedElement = parentElement.querySelector('.received');

            listeningElement.setAttribute('style', 'display:none;');
            receivedElement.setAttribute('style', 'display:block;');

            console.log('Received Event: ' + id);
        }
};

app.initialize();

当时没有发生任何事件。因此,我将监听器移动到“绑定事件”功能。

    var app = {
        // Application Constructor
        initialize: function() {
            this.bindEvents();
        },

        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
            document.addEventListener("online", function(){alert('online : true') }, false);
            document.addEventListener("offline", function(){alert('online : true') }, false);
        },

        onDeviceReady: function() {
            app.receivedEvent('deviceready');
            // Here, we redirect to the web site.
            var targetUrl = "https://sputnik.geoscan.aero/";
            var bkpLink = document.getElementById("bkpLink");
            bkpLink.setAttribute("href", targetUrl);
            bkpLink.text = targetUrl;
            window.location.replace(targetUrl);
            },

        receivedEvent: function(id) {
            var parentElement = document.getElementById(id);
            var listeningElement = parentElement.querySelector('.listening');
            var receivedElement = parentElement.querySelector('.received');

            listeningElement.setAttribute('style', 'display:none;');
            receivedElement.setAttribute('style', 'display:block;');

            console.log('Received Event: ' + id);
        }
};

app.initialize();

现在,只有在网页加载之前的应用程序启动时,在线事件才会上升,而且永远不会再次出现。当我打开和关闭Wi-Fi后没有任何反应。我真的无法理解发生了什么。

0 个答案:

没有答案