Phonegap活动在线/离线无效

时间:2013-08-10 10:09:55

标签: javascript ios cordova cordova-3

我正在使用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);
    }
};

感谢建议

5 个答案:

答案 0 :(得分:8)

如果要显示在线/离线状态,则需要先使用命令提示符

添加网络信息插件
$ phonegap local plugin add org.apache.cordova.network-information

添加该插件后,您的在线/离线活动应该有效,它对我来说很好用

答案 1 :(得分:2)

这些事件必须绑定在“onDeviceReady”中,它们在DeviceReady事件之前不起作用。请检查此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);
},

请注意,应用启动时不会触发在线/离线事件,这些事件仅在连接状态发生变化时触发。假设当应用程序以在线模式启动时,直到它脱机,将不会触发离线事件,对于在线事件也是如此。

要检查当前连接,您需要使用以下代码

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

答案 2 :(得分:2)

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

答案 3 :(得分:0)

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

添加Connection插件使用以下命令:

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

答案 4 :(得分: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>