Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set event listeners in Cordova plugin

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!

like image 440
Rene Padillo Avatar asked Jan 23 '16 12:01

Rene Padillo


People also ask

What is Cordova event listener?

Cordova provides a set of various events for your application. These events are fired by the application, if required. We know that sometimes, we often need to determine when our application enters the device background and when it comes back into the foreground. So, for such tasks, we have event listeners.

How do I use Cordova plugins?

First, you have to pass the plugin-spec command in the command prompt. Then, plugin-spec is saved into the xml and package. json file of an app. After saving the file, we can publish the latest plugin version to npm that our current project supports.

Is Cordova discontinued?

Apache Cordova Is Retired: Alternatives for Cross Platform Mobile Development in 2022. Future trends of cross-platform mobile development are already starting to emerge, and it appears that Apache Cordova won't be included in the list of frameworks that power hybrid web apps for mobile devices.

What is event listener in Dom?

The addEventListener() method allows you to add event listeners on any HTML DOM object such as HTML elements, the HTML document, the window object, or other objects that support events, like the xmlHttpRequest object.


1 Answers

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.

like image 161
beaver Avatar answered Oct 02 '22 18:10

beaver