5

I have created one java class which extends CordovaPlugin.

For eg,

public class SampleCardovaPlugin extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("echo")) {
            String message = args.getString(0); 
            this.echo(message, callbackContext);
            return true;
        }
        return false;
    }
private void echo(String message, CallbackContext callbackContext) {
    if (message != null && message.length() > 0) { 
        callbackContext.success(message);
    } else {
        callbackContext.error("Expected one non-empty string argument.");
    }
}

}

I'm using cordova2.5.0.

How would I call this plugin from my javascript function? Please do the needful.

1

2 Answers 2

3

You must first register your plugin in the config.xml in the res folder.

Then in the javascript:

cordova.exec(
    function(winParam) {},
    function(error) {},
    "service",
    "action",
    ["firstArgument", "secondArgument", 42, false]);

so in your case

cordova.exec(
    function(data) { console.log(data);},
    function(error) { console.log(error);},
    "SampleCardovaPlugin",
    "echo",
    ["echo"]);

You must also be sure that the device is ready

take a look at http://docs.phonegap.com/en/2.0.0/guide_plugin-development_index.md.html

Sign up to request clarification or add additional context in comments.

Comments

0

You have to create plugin (java class, package.json, plugin.xml ...) There is no easier way how to do it. But you can create really simple one (much easier then that one from documentation) -> read more here on my blog.

Then you call it in JS like 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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.