javascript 在线/离线Phonegap事件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18160866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:55:45  来源:igfitidea点击:

Phonegap events online/offline not working

javascriptioscordovacordova-3

提问by user2374693

I am writing app with phonegap(cordova) 3.0.0 and events "online" and "offline" doesn't work. When I tried event "resume", this event was OK. I am using XCode 4.5 and IOS.

我正在使用 phonegap(cordova) 3.0.0 编写应用程序,并且“在线”和“离线”事件不起作用。当我尝试事件“恢复”时,这个事件没问题。我正在使用 XCode 4.5 和 IOS。

This is my main javascript file of phonegap project:

这是我的 phonegap 项目的主要 javascript 文件:

var app = {

    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        document.addEventListener('online', this.onDeviceOnline, false);
        document.addEventListener('resume', this.onDeviceResume, false);
    },

    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },

    onDeviceOnline: function() {
        app.receivedEvent('deviceonline');
    },

    onDeviceResume: function() {
        app.receivedEvent('deviceresume');
    },

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

Thank for advices

谢谢建议

回答by FnD

if you want to display online / offline status you need to add network-information plugin first with command prompt

如果你想显示在线/离线状态,你需要先在命令提示符下添加网络信息插件

$ phonegap local plugin add org.apache.cordova.network-information

after adding that plugin your online / offline event should work, it work fine for me

添加该插件后,您的在线/离线事件应该可以正常工作,对我来说效果很好

回答by Ashis Kumar

These events has to be bind inside "onDeviceReady", they will not work before the DeviceReady event. Check this Attach an event listener once the deviceready event fires

这些事件必须绑定在“onDeviceReady”中,在 DeviceReady 事件之前它们将不起作用。一旦 deviceready 事件触发,请检查此附加事件侦听器

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

onDeviceReady: function() {
    app.receivedEvent('deviceready');
    document.addEventListener('online', this.onDeviceOnline, false);
},

Please note that online/offline event is not fired when the app starts, these event only get fired when connectivity state changes. Let say when app starts in online mode, until it goes offline, offline event will not be triggered, same for online event.

请注意,应用程序启动时不会触发在线/离线事件,这些事件仅在连接状态更改时触发。假设当应用程序以在线模式启动时,直到它下线,才会触发离线事件,在线事件也是如此。

To check the current connectivity, you need to use the below code

要检查当前的连通性,您需要使用以下代码

onDeviceReady: function() {
    app.receivedEvent('deviceready');
    document.addEventListener('online', this.onDeviceOnline, false);
    if((navigator.network.connection.type).toUpperCase() != "NONE" &&
       (navigator.network.connection.type).toUpperCase() != "UNKNOWN") {
        this.onDeviceOnline();
    }
}

回答by Nicramus

In corodova (not phonegap) you have to add plugin in this way:
cordova plugin add org.apache.cordova.network-information

在 corodova(不是 phonegap)中,您必须以这种方式添加插件:
cordova plugin add org.apache.cordova.network-information

回答by lepix

In the phonegap folder project:

在 phonegap 文件夹项目中:

phonegap plugin add org.apache.cordova.network-information

In index.js:

index.js

var app = {};
app.initialize = function() {
    document.addEventListener("online", function(){alert('online : true') }, false);
    document.addEventListener("offline", function(){alert('online : false') }, false);
};

In index.html, somewhere :

index.html,某处:

<script type="text/javascript">
app.initialize();
</script>

回答by Hadi Mostafapour

you should add Connection plugin to your project and then this events will be fired.

您应该将 Connection 插件添加到您的项目中,然后将触发此事件。

to add Connection plugin use following command:

添加连接插件使用以下命令:

CMD> phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git