英文:
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("cordova/exec");
module.exports.mathCalculator = function (arg0, success, error) {
cordova.exec(success, error, "MathCalculator", "add", [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("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");
}
}
}
And in config.xml (I paste here just what is inside <platform name="android"></platform>
<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>
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, "MathCalculator", "add", [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 <js-module ...><clobbers target="mathCalculator"></clobbers></js-module>
, I can call my plugin directly with mathCalculator name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论