创建 Cordova 插件

huangapple go评论86阅读模式
英文:

Create plugin for cordova

问题

我尝试创建一个Cordova插件。
我使用plugman创建了插件文件。
以下是MathCalculator.js中的内容:

var exec = require("cordova/exec");

module.exports.mathCalculator = function (arg0, success, error) {
  cordova.exec(success, error, "MathCalculator", "add", [arg0]);
};

在MathCalculator.java中:

package cordova.plugin.mathcalculator;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * This class echoes a string called from JavaScript.
 */
public class MathCalculator extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if(action.equals("add")) {
            this.add(args, callbackContext);
            return true;
        } else if(action.equals("substract")) {
            this.substract(args, callbackContext);
            return true;
        }

        return false;
    }

    private void add(JSONArray args, CallbackContext callback) {
        if(args != null) {
            try {
                int p1 = Integer.parseInt(args.getJSONObject(0).getString("param1"));
                int p2 = Integer.parseInt(args.getJSONObject(0).getString("param2"));

                callback.success("" + (p1 + p2));
            } catch (Exception ex) {
                callback.error("Something went wrong" + ex);
            }    
        } else {
            callback.error("Please do not pass null value");
        }
    }

    private void substract(JSONArray args, CallbackContext callback) {
        if(args != null) {
            try {
                int p1 = Integer.parseInt(args.getJSONObject(0).getString("param1"));
                int p2 = Integer.parseInt(args.getJSONObject(0).getString("param2"));

                callback.success("" + (p1 - p2));
            } catch (Exception ex) {
                callback.error("Something went wrong" + ex);
            }    
        } else {
            callback.error("Please do not pass null value");
        }
    }
}

在config.xml中(我只粘贴了位于<platform name="android">内的部分):

<platform name="android">
    <config-file parent="/*" target="res/xml/config.xml">
        <feature name="MathCalculator">
            <param name="android-package" value="cordova.plugin.mathcalculator.MathCalculator" />
        </feature>
    </config-file>
    <config-file parent="/*" target="AndroidManifest.xml" />
    <source-file src="src/android/MathCalculator.java" target-dir="src/cordova/plugin/mathcalculator/MathCalculator" />
</platform>

在index.js中,尝试调用该插件(在添加插件后):

mathCalculator.add({param1: 1, param2: 3});

但是,mathCalculator未定义。我导出了mathCalculator

(注意:你的代码中有一些HTML转义字符,已在翻译中去除,但这可能会导致问题。确保你的代码中没有这些转义字符。)

英文:

I try to make a cordova plugin.
I created plugin files with plugman.
This is what I have in MathCalculator.js

var exec = require(&quot;cordova/exec&quot;);
module.exports.mathCalculator = function (arg0, success, error) {
cordova.exec(success, error, &quot;MathCalculator&quot;, &quot;add&quot;, [arg0]);
};

In MathCalculator.java I have:

package cordova.plugin.mathcalculator;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* This class echoes a string called from JavaScript.
*/
public class MathCalculator extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if(action.equals(&quot;add&quot;)) {
this.add(args, callbackContext);
return true;
} else if(action.equals(&quot;substract&quot;)) {
this.substract(args, callbackContext);
return true;
}
return false;
}
private void add(JSONArray args, CallbackContext callback) {
if(args != null) {
try {
int p1 = Integer.parseInt(args.getJSONObject(0).getString(&quot;param1&quot;));
int p2 = Integer.parseInt(args.getJSONObject(0).getString(&quot;param2&quot;));
callback.success(&quot;&quot; + (p1 + p2));
} catch (Exception ex) {
callback.error(&quot;Something went wrong&quot; + ex);
}    
} else {
callback.error(&quot;Please do not pass null value&quot;);
}
}
private void substract(JSONArray args, CallbackContext callback) {
if(args != null) {
try {
int p1 = Integer.parseInt(args.getJSONObject(0).getString(&quot;param1&quot;));
int p2 = Integer.parseInt(args.getJSONObject(0).getString(&quot;param2&quot;));
callback.success(&quot;&quot; + (p1 - p2));
} catch (Exception ex) {
callback.error(&quot;Something went wrong&quot; + ex);
}    
} else {
callback.error(&quot;Please do not pass null value&quot;);
}
}
}

And in config.xml (I paste here just what is inside <platform name="android"></platform>

&lt;platform name=&quot;android&quot;&gt;
&lt;config-file parent=&quot;/*&quot; target=&quot;res/xml/config.xml&quot;&gt;
&lt;feature name=&quot;MathCalculator&quot;&gt;
&lt;param name=&quot;android-package&quot; value=&quot;cordova.plugin.mathcalculator.MathCalculator&quot; /&gt;
&lt;/feature&gt;
&lt;/config-file&gt;
&lt;config-file parent=&quot;/*&quot; target=&quot;AndroidManifest.xml&quot; /&gt;
&lt;source-file src=&quot;src/android/MathCalculator.java&quot; target-dir=&quot;src/cordova/plugin/mathcalculator/MathCalculator&quot; /&gt;
&lt;/platform&gt;

I tried to call this plugin (after add it) in index.js like this:

mathCalculator.add({param1: 1, param2: 3});

But, mathCalculator is not defined.
I exported mathCalculator.

答案1

得分: 0

我使用plugman创建了插件,然后在bridge(MathCalculator.js)中添加了以下内容:

mathCalculator = {
    myMethod: function (options, success, error) {
        cordova.exec(success, error, "MathCalculator", "add", [options]);
    }
};

module.exports = mathCalculator;

然后,在cordova中,我可以像这样调用这个方法:cordova.plugins.mathCalculator.myMethod();

如果我在插件xml中添加了<js-module ...><clobbers target="mathCalculator"></clobbers></js-module>,我可以直接使用mathCalculator名称调用我的插件。

英文:

I created the plugin with plugman, then I added in bridge (MathCalculator.js) var

mathCalculator = myMethod: function (options, success, error) {
cordova.exec(success, error, &quot;MathCalculator&quot;, &quot;add&quot;, [options]);
},
module.exports = mathCalculator;

Then, in cordova I can call this method like this: cordova.plugins.mathCalculator.myMethod();

If I add in plugin xml insite &lt;js-module ...&gt;&lt;clobbers target=&quot;mathCalculator&quot;&gt;&lt;/clobbers&gt;&lt;/js-module&gt;, I can call my plugin directly with mathCalculator name.

huangapple
  • 本文由 发表于 2020年8月10日 01:10:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63329171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定