Question

I am trying to show and alert when the connection status changes but my code has not effect at all (alert below is not being executed).

Here is it:

var app = {
    // Application Constructor
    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);
    },
    // 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() {
        app.receivedEvent('deviceready');

        document.addEventListener("online", onOnline, false);
        document.addEventListener("offline", onOffline, false); 
    },
    // Update DOM on a Received Event
    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);
    }


};

function onOffline() {
    alert("on");
}

function onOffline() {
    alert("off");
}

My PhoneGap version is 3.3.0-0.18.0. I am targeting Android so I generated the project like this:

phonegap create my-app
cd my-app
phonegap run android

What I am missing?

Was it helpful?

Solution

Since version 3.0 you don't have to manually add permissions to AndroidManifest.xml. On page http://docs.phonegap.com/en/3.3.0/cordova_connection_connection.md.html#Connection you can read:

As of version 3.0, Cordova implements device-level APIs as plugins. Use the CLI's plugin command, described in The Command-Line Interface, to add or remove this feature for a project:

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

If you don't use the phonegap build service you can use cordova instead of phonegap (which is built on top of cordova).

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top