How to set event listeners in Cordova plugin
Asked Answered
S

1

6

I would like to ask on how you can set your own event listener in your Cordova plugin.

I have this Share dialog for my Android and I wanted to have my Javascript to set a listener for onShareDialogDismiss or onShareDialogLaunched

What would likely to happen in Javascript would look like this.

// Set a listener for dialog dismiss
document.addEventListener('onShareDialogDismiss', listenerCallback, false);

// Set a listener for dialog launch
document.addEventListener('onShareDialogLaunch', launchCallback, false);

I have this code from Titanium, but it's using TiViewProxy class, would likely to know how you could do an alternative for fireEvent() in pure Android implementation

Thanks!

Salerno answered 23/1, 2016 at 12:40 Comment(0)
G
6

Firstly have you already read Cordova plugin development documentation?

Then you can see how a plugin like this cordova-plugin-network-information is done :

Check for example the JS interface code in which there are these code lines to raise a Document event:

cordova.fireDocumentEvent("offline");

or

cordova.fireDocumentEvent("online");

Reading inside cordova.js there is a minimal documentation for this API:

/**
 * Method to fire event from native code
 * bNoDetach is required for events which cause an exception which needs to be caught in native code
 */
fireDocumentEvent: function(type, data, bNoDetach)

Another API available is fireWindowEvent: function(type, data), but you can find out other APIs reading directly inside cordova.js.

Goodden answered 26/1, 2016 at 10:49 Comment(2)
The network info plugin features a rather cumbersome implementation of "events" by using a timer that runs every half second to check if the status has changed. There should be a smarter way to trigger events without using Javascript timers, in fact this is a "cheating" method, not a real event-trigger mechanism.Endomorphic
I agree @andreszs. Something like a BroadcastReceiver (with filter on ConnectivityManager.CONNECTIVITY_ACTION) should have been used.Plaided

© 2022 - 2024 — McMap. All rights reserved.