Matthew McCoy
Matthew McCoy

Reputation: 63

Creating a helloWorld plugin for Android using Cordova and Eclipse

I've done quite a bit of research and can't seem to find why this isn't working. What I have is Cordova based Android app in Eclipse running Cordova 2.7.0. I want to build a simple plugin that just alerts the user when it has completed.

My index.html

    <head>
    <script type="text/javascript" src="cordova-2.7.0.js"></script>
    <script>
        window.func = function(str,callback){
            alert("Outside Call Working");
            cordova.exec(callback, function(err){alert(err)},"HelloPlugin","echo", [str]);
        }
        function callPlugin(str){
            alert("JS Working");
            window.func(str,function(){
                alert("Done!");
            });
        }
    </script>
</head>
<body>
    <h2>PluginTest</h2>
    <a onclick="callPlugin('Plugin Working!')">Click me</a>
</body>

My config.xml line where I add the plugin

<plugin name="HelloPlugin" value="org.apache.cordova.plugin.HelloPlugin" />

And my actual plugin HelloPlugin.java that is in src/com/example/plugintest right next to MainActivity.java

package com.example.plugintest;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

public class HelloPlugin extends CordovaPlugin{

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        /*if(action.equals("echo")){
            String message = args.getString(0); 
            callbackContext.success(message);
            return true;
        }*/
        callbackContext.success(action);
        return true;
    }
}

Any help is greatly appreciated!

Upvotes: 4

Views: 15447

Answers (2)

Eranga Perera
Eranga Perera

Reputation: 938

In this line

    window.func = function(str,callback){
        alert("Outside Call Working");
        cordova.exec(callback, function(err){alert(err)},"HelloPlugin","echo", [str]);
    }

put like this

window.func = function(str,callback){
        alert("Outside Call Working");
        cordova.exec(callback, function(err){alert(err)},"org.apache.cordova.plugin.HelloPlugin","echo", [str]);
    }

Upvotes: 4

MBillau
MBillau

Reputation: 5376

The value of "HelloPlugin" in your config.xml should point to the package where the Java class is, so that Cordova can find and execute the Java code. So if you change <plugin name="HelloPlugin" value="org.apache.cordova.plugin.HelloPlugin" /> to <plugin name="HelloPlugin" value="com.example.plugintest.HelloPlugin" /> I believe it should work.

Upvotes: 3

Related Questions