Phonegap offline / online event not working

I am working on a telephone (using 2.7), and I am having problems with an online and offline event. It seems to be not working as indicated in the docs.

My code is to call the deviceready event.

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 of the listeners do not start. I tried to put a delay in calling online listeners (1500 ms), but still to no avail. I also tried placing the console log inside the deviceConnectionListener to ensure that the deviceready event occurs.

Does anyone have an idea on a workaround or have this problem?

Thanks!

+4
source share
3 answers

I ran into a similar problem with Cordova 3.1 on iOS 6. Although not documented, install the Connection plugin first. After installation is complete, you can configure event handlers.

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

Yes, @Orijit is right. It is not documented and to use online events / offline events you need to:

1) add PhoneGap plugin to connect

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

2) change config.xml and AndroidManifest.xml:

 (in app/res/xml/config.xml) <feature name="NetworkStatus"> <param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" /> </feature> (in app/AndroidManifest.xml) <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

3) use events online / offline as described above

4) create a new assembly for example Android: corova build android


Docs

Connection plugin:

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

online / offline events:

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

+2
source

Online / offline events are associated with the Phonegap connectivity API. Since the emulator does not support this API like other APIs (for example, Accelerometer, Compass, etc.), Therefore, these online events will not be triggered in the emulator.

But if you run the application on an Andriod phone with a version greater 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.

+1
source

Source: https://habr.com/ru/post/1493552/


All Articles