Creating a helloWorld plugin for Android using Cordova and Eclipse

I have done quite a bit of research and cannot find why this is not working. I have a Cordoba-based Android application in Eclipse running Cordova 2.7.0. I want to create a simple plugin that just warns the user about its completion.

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 line is config.xml where I add the plugin

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

And my actual HelloPlugin.java plugin, which is located 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 much appreciated!

+4
source share
2 answers

The value "HelloPlugin" in your config.xml should point to the package where the Java class is located, so Cordoba can find and execute 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 consider That should work.

+3
source

In this line

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

enter

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

Source: https://habr.com/ru/post/1481825/


All Articles