7 Adding Custom Cordova Plugin
This topic provides information on Adding Custom Cordova Plugin.
Step 1 -
Create java folder and add yout package under app(zigbank\platforms\android\app)
Create java file under your package which will extends CordovaPlugin
Override execute method with JsonArray as a parameter
Retrive jsonobject from JsonArray and get the data which passed from js file
Example:
public class GetDirectionMapPlugin extends CordovaPlugin
{
@Override
public boolean execute
(String action, JSONArray args,CallbackContext callbackContext)
throws JSONException {
try{
JSONObject object = args.getJSONObject(0);
String yourKey = object.getString("your_key");
}catch (Exception e)
{
Log.e(TAG,e.getMessage());
}
return true;
}
}
Step 2 –
Create plugin file under plugins folder of
www(zigbank\platforms\android\service\workspace\app\src\main\assets\www\plugins)
Example:
cordova.define("cordova-plugin-getdirection", function(require, exports, module)
{
var exec = cordova.require('cordova/exec');
exports.navigate = function(args, successCallback, errorCallback)
{
cordova.exec(successCallback, errorCallback,"GetDirectionMapPlugin", "direction",
[args]);
};
});
cordova-plugin-getdirection.getDirectionPlugin → user defined id from
cordova_plugin.js(zigbank\platforms\android\service\workspace\app\src\main\assets\ww
w\cordova_plugin.js)
GetDirectionMapPlugin→ name of java plugin class
direction → action
navigate → this can be use in js file to this function
Step 3 –
Make entry of plugin in
cordova_plugin.js(zigbank\platforms\android\service\workspace\zigbank\platforms\android\app\sr
c\main\assets\www) as below →
Example:
{
"id": "cordova-plugin-getdirection.getDirectionPlugin",
-> user defined id
"file":"plugins/cordova-plugin-getdirection/www/mapgetdirection.js", -> path of plugin
js
file
"pluginId":"cordova-plugin-getdirection",
"clobbers": [
"window.getDirection" -> this can be used in js file to call plugin
]
}
Step 4 -
Make entry of java plugin class in
config.xml(zigbank\platforms\android\service\workspace\zigbank\platforms\android\app\src\main\r
es\xml) file of app as below →
Example:
<feature name="GetDirectionMapPlugin">
<param name="android-package" value="Your_Plugin_Java_Class_Path" />
</feature>
GetDirectionMapPlugin -> Name of java plugin class
Step 5 -
Plugin calling in js file →
Example:
window.getDirection.navigate
({
originLatLng: origin,
destinationLatLng: location
})
window.getDirection -> clobber define in the cordova_plugin.js file
navigate → name of the function defined in plugin js file