How to call cordova android function from javascript?

cordova plugin example

TL;DR

You have to create plugin. There is no easier way how to do it. But you can create really simple one (much easier then that one from documentation) so continue reading!

Calling Android function from JavaScript

Let’s start with the easy part, the JavaScript part. To call your Android function all you have to do is this.

That’s nice but you need the Cordova plugin to do that so let’s create one!

How to create Cordova plugin

This is the hardest part and when you realize, you are creating plugin to call one function on the android side it’s just … So what are we doing? We are going to create Android plugin that will allow us to receive calls from Javascript .

cordova-example-plugin

├── package.json
├── plugin.xml
├── src
│   └── android
│       └── Example.java
└── www
    └── Example.js

Simple right? And you want it simpler then simple? Here it is ready to clone/download and rename.

DOWNLOAD EXAMPLE FILES

So what do we have here? Let’s look at Example.java. Do you remember the javascript part? Here we have Example class (3rd parameter) then our action (4th parameter) and our arguments are accessible in args parameter with args.getString(0).

Example.java

package.json

Example.js is empty. In package.json we have info about our plugin.

plugin.xml

in plugin.xml we have another info about our plugin. But notice here the line. In this file we tell Cordova about permissions we are asking for. (Internet permission is here just for the sakes of example)

Installation

What now? Now install it! Go to your cordova project and just copy paste the plugin there. Then run (in your project folder):

cordova plugin add ./cordova-example-plugin

You can have the plugin wherever you want, it doesn’t have to be in you project path, but remember that may come time when someone else will be pulling your project from repository and he will have your plugin as a dependency.

References

 

Related Post

Right Click to Create New File on Mac OS X TL;DR; Windows has a lot of cons but it has also some pros. One of them is a smooth way to create empty file on right-click. Mac OS doesn't come with...
MacBook wifi issue – OS X keeps reconnecting Last few weeks I was facing annoying wifi problems. My mac was reconnecting every 5 minutes. On top of that BSSID was reseting so I saw BSSID changing...
FAQ: Mac OS X Services HEIC to JPG on Right-Click Service Q: Where’d all my services go? I don’t see anything in the Services menu. A: If you don’t select anything before...
Right-Click to Translate (Look Up) with Google Tra... If you are not native english speaker, you may be frustrated with default "look up" options you get after selecting a text and right clicking. There a...
Free Better Battery Stats and Time Remaining for M... Better Battery Stats and Time Remaining for Mac OS TL;DR; Do you miss the time remaining indicator on your MacBook, MacBook Air, or MacBook Pro? He...
Convert HEIC to PNG on Right Click for Free Convert HEIC to PNG on Right-Click In iOS 11 Apple is replacing the PNG image format with the new HEIC alternative. But what to do when device or pr...
Buy me a coffeeOut of coffee 😱, please help!