How to call cordova android function from javascript?
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.
cordova.exec(function(success) {}, //success callback | |
function(error) {}, //error callback | |
"Example", //class name | |
"YOUR_ACTION_NAME_PARAMETER", //action name | |
["Dog", "Pig", 42, false]); //args |
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.
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 com.filipmolcik.plugin; // This must match with the plugin.xml and package.json | |
import org.apache.cordova.CallbackContext; | |
import org.apache.cordova.CordovaPlugin; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
// Example is the 3rd parameter in javascript part | |
public class Example extends CordovaPlugin { | |
@Override // action is the 4th parameter, args is the array from 5th parameter in javascript part | |
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { | |
if (action.equals("YOUR_ACTION_NAME_PARAMETER")) { | |
// Your black magic comes here | |
return true; | |
} | |
return false; // Returning false results in a "MethodNotFound" error. | |
} | |
} |
package.json
Example.js is empty. In package.json we have info about our plugin.
{ | |
"name": "com.filipmolcik.plugin.example", | |
"version": "1.0.0", | |
"description": "Example plugin to demonstrate how to call android function from javascript.", | |
"cordova": { | |
"id": "com.filipmolcik.plugin.example", | |
"platforms": [ | |
"android" | |
] | |
}, | |
"keywords": [ | |
"ecosystem:cordova", | |
"cordova-android" | |
], | |
"author": "Filip Molcik", | |
"license": "null" | |
} |
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)
<?xml version="1.0" encoding="UTF-8"?> | |
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" | |
id="com.filipmolcik.plugin.example" | |
version="1.0.0"> | |
<name>filipmolcik-push-notifications</name> | |
<description>subscribe for push notifications</description> | |
<license>null</license> | |
<js-module src="www/Example.js" name="Example"> | |
<clobbers target="Example" /> | |
</js-module> | |
<platform name="android"> | |
<config-file target="res/xml/config.xml" parent="/*"> | |
<feature name="Example" > | |
<param name="android-package" value="com.filipmolcik.plugin.Example"/> | |
</feature> | |
</config-file> | |
<config-file target="AndroidManifest.xml" parent="/*"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
</config-file> | |
<source-file src="src/android/Example.java" target-dir="src/com/filipmolcik/plugin" /> | |
</platform> | |
</plugin> |
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
- [1] https://cordova.apache.org/docs/en/latest/guide/hybrid/plugins/
- [2] https://cordova.apache.org/docs/en/latest/guide/platforms/android/plugin.html