使用Cordova和Eclipse创建Android的helloWorld插件 [英] Creating a helloWorld plugin for Android using Cordova and Eclipse
问题描述
我做了相当多的研究,似乎找不到为什么这不工作。我有基于Cordova的Android应用程序在Eclipse中运行Cordova 2.7.0。我想构建一个简单的插件,只是在用户完成后提醒用户。
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.
我的index.html
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>
我添加插件的config.xml行
My config.xml line where I add the plugin
<plugin name="HelloPlugin" value="org.apache.cordova.plugin.HelloPlugin" />
而我的实际插件HelloPlugin.java在src / com / example / plugintest紧挨MainActivity .java
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;
}
}
任何帮助都非常感谢!
推荐答案
config.xml中的HelloPlugin的值应该指向Java类所在的包,以便Cordova可以找到并执行Java代码。因此,如果您更改
< plugin name =HelloPluginvalue =org.apache.cordova.plugin.HelloPlugin/>
< plugin name =HelloPluginvalue =com.example.plugintest.HelloPlugin/>
我相信它应该工作。
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.
这篇关于使用Cordova和Eclipse创建Android的helloWorld插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!