使用 Cordova 和 Eclipse 为 Android 创建一个 helloWorld 插件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16657233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:32:31  来源:igfitidea点击:

Creating a helloWorld plugin for Android using Cordova and Eclipse

androideclipsepluginscordova

提问by Matthew McCoy

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.

我做了很多研究,似乎无法找到为什么这不起作用。我拥有的是运行 Cordova 2.7.0 的 Eclipse 中基于 Cordova 的 Android 应用程序。我想构建一个简单的插件,当它完成时只提醒用户。

My index.html

我的 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

我添加插件的 config.xml 行

<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

而我的实际插件 HelloPlugin.java 就在 MainActivity.java 旁边的 src/com/example/plugintest 中

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!

任何帮助是极大的赞赏!

采纳答案by MBillau

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.

config.xml 中“HelloPlugin”的值应该指向Java 类所在的包,以便Cordova 可以找到并执行Java 代码。因此,如果您更改 <plugin name="HelloPlugin" value="org.apache.cordova.plugin.HelloPlugin" /><plugin name="HelloPlugin" value="com.example.plugintest.HelloPlugin" />我相信它应该可以工作。

回答by Eranga Lakmal Perera

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]);
    }