admin管理员组

文章数量:1393920

I'm working on a phonegap application (using 2.7) and I'm having a trouble at the "online" and "offline" event. Seems that its not working as stated on the docs.

My code consists of calling the deviceready event first.

function init(){
  document.addEventListener('deviceready', arrangeConnectionListener, false);  
}

function arrangeConnectionListener(){
  document.addEventListener('online', onOnline, false);
  document.addEventListener('offline', onOffline, false);
}

$(document).ready(init);

However, the functions on the listeners are not being triggered. I tried to place a delay in calling the online/offline listeners (1500ms) but still to no avail. I also tried placing a console log inside the arrangeConnectionListener to ensure that the deviceready event is firing.

Anyone have an idea on a workaround or experienced this issue?

Thanks!

I'm working on a phonegap application (using 2.7) and I'm having a trouble at the "online" and "offline" event. Seems that its not working as stated on the docs.

My code consists of calling the deviceready event first.

function init(){
  document.addEventListener('deviceready', arrangeConnectionListener, false);  
}

function arrangeConnectionListener(){
  document.addEventListener('online', onOnline, false);
  document.addEventListener('offline', onOffline, false);
}

$(document).ready(init);

However, the functions on the listeners are not being triggered. I tried to place a delay in calling the online/offline listeners (1500ms) but still to no avail. I also tried placing a console log inside the arrangeConnectionListener to ensure that the deviceready event is firing.

Anyone have an idea on a workaround or experienced this issue?

Thanks!

Share Improve this question asked Jul 26, 2013 at 1:35 jasonjason 811 silver badge6 bronze badges 8
  • have you tried their example in the docs? docs.phonegap./en/1.0.0/… – CBIII Commented Jul 26, 2013 at 1:41
  • yes. still doesn't work though. :( – jason Commented Jul 26, 2013 at 1:51
  • what device are you using? Also I linked to a very old version I meant this one It might not make a difference though – CBIII Commented Jul 26, 2013 at 1:52
  • im running an ios emulator. yesterday though i tested on amazon kindle fire, Ipad2 and galaxy tab devices and yes, still not working. after some changes in my code now, it works on the emulator. I'll try to pile it first in adobe builder, test in the devices, and will let you know what happens. – jason Commented Jul 26, 2013 at 1:55
  • 2 As of cordova/phonegap 3+ This is enabled when you install the network plugin phonegap plugin add org.apache.cordovawork-information. After you've done this, you can use HTML5 online/offline events +1 Orjit – naikus Commented Jan 9, 2014 at 12:43
 |  Show 3 more ments

4 Answers 4

Reset to default 3

I faced similar issue with Cordova 3.1 on IOS 6. Though not mentioned in the documentation clearly, install the Connection plugin first. After installation is done, you can setup your event handlers.

    document.addEventListener("deviceready", onDeviceReady, false);
    document.addEventListener("online", onOnline, false);
    document.addEventListener("offline", onOffline, false);

Yes, @Orijit is right. It's not well documented and in order to use the online / offline events you need to :

1) add the "Connection" Phonegap plugin

$ cordova plugin add org.apache.cordovawork-information

2) modify config.xml and AndroidManifest.xml:

(in app/res/xml/config.xml)
<feature name="NetworkStatus">
    <param name="android-package" value="org.apache.cordovaworkinformation.NetworkManager" />
</feature>    

(in app/AndroidManifest.xml)
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

3) use the online/offline events as explained above

4) create a new build for example, Android: cordova build android


DOCS

Connection plugin:

http://docs.phonegap./en/3.3.0/cordova_connection_connection.md.html#Connection

online/offline events:

http://docs.phonegap./en/3.3.0/cordova_events_events.md.html#online

Online/Offline Events are related to Connection API of the Phonegap. As Emulator does not support this API similar like other APIs(e.g. Accelerometer, Compass etc.) Hence this online/offline events will not triggered on Emulator.

But if you run your application on Andriod phone having version more than 4.3.0, it will be executed. -copy .apk file in D:/myphonegap/project_name/bin/project_name.apk and -install on your andriod phone.

window.addEventListener

instead of

window.document.addEventListener

本文标签: javascriptphonegap offlineonline event not workingStack Overflow