6

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

这是我的 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);
    }
};

感谢您的建议

4

5 回答 5

8

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

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

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

于 2013-11-05T14:05:29.517 回答
2

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

于 2014-02-26T13:23:29.773 回答
2

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

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();
    }
}
于 2013-08-10T10:13:50.150 回答
0

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

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

CMD> phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git
于 2013-08-16T18:03:54.903 回答
0

在 phonegap 文件夹项目中:

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

index.js

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

index.html,某处:

<script type="text/javascript">
app.initialize();
</script>
于 2015-02-25T11:39:58.410 回答