I am quite new in cordova and android native code
I had created my own plugin by using plugman.
plugman create --name myCustomPlugin --plugin_id myCustomPlugin --plugin_version 1.0
plugman had generated all the needed files for me to get started.
js file www/myCustomPlugin.js
var exec = require('cordova/exec');
exports.coolMethod = function(arg0, success, error) {
exec(success, error, "myCustomPlugin", "coolMethod", [arg0]);
};
java file src/android/myCustomPlugin.java
, method execute
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("coolMethod")) {
String message = args.getString(0);
this.coolMethod(message, callbackContext);
return true;
}
return false;
}
private void coolMethod(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
callbackContext.success(message);
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
The question is: On the cordova project js file. How do I call myCustomPlugin? and how do I know that it is working?
I am using angular in my cordova project, let say my controller is like this:
.controller('mainCtrl', function($scope){
// how to call myCustomPlugin here?
});
My motive is just to learn how to call and how it work only, maybe after click it will do a native alert or open native view or something.
Thanks in advance!
You can do:
www/myCustomPlugin.js
var MyCustomPlugin {
coolMethod : function(arg0, success, error) {
exec(success, error, "myCustomPlugin", "coolMethod", [arg0]);
}
}
module.exports = MyCustomPlugin
And in your js client just:
MyCustomPlugin.coolMethod("Arg1", function(){}, function(){});
Cordova will inject JavaScript file for you.
Hope it helps.