拥有cordova插件类undefined

时间:2017-03-10 15:28:10

标签: android cordova cordova-plugins

我最近正在开发自己的用于android的cordova插件。到目前为止,我在plugin.xml上有这个

<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-my-test" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>myTest</name>
<js-module name="myTest" src="www/myTest.js">
    <clobbers target="myTest" />
</js-module>
<platform name="android">
    <config-file parent="/*" target="res/xml/config.xml">
        <feature name="myTest">
            <param name="android-package" value="nl.tester.test.myTest.myTest" />
        </feature>
    </config-file>
    <config-file parent="/*" target="AndroidManifest.xml"></config-file>
    <source-file src="src/android/myTest.java" target-dir="src/nl/tester/test/myTest" />
</platform>

这是www /

中的myTest.js
module.exports = {
greet: function(message, successCallback, errorCallback) {
cordova.exec(successCallback,
             errorCallback, // No failure callback
             "myTest",
             "greet",
             [message]);
      }
};

这是myTest.java

package nl.tester.test.myTest;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;

public class myTest extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {

    if (action.equals("greet")) {

        String name = data.getString(0);
        String message = "Hello, " + name;
        this.displaytext(message, callbackContext);

        return true;

    }
    return false;
}

private void displaytext(String message, CallbackContext callbackContext) {
    if (message != null && message.length() > 0) {
        callbackContext.success(message);
    } else {
        callbackContext.error("Expected one non-empty string argument.");
    }
}
}

这似乎是一个非常简单的例子。但是,我无法使其发挥作用。所以在我的www / js / index.js中,当我调用myTest.greet('World', success, failed)时,我得到了 undefined ,并为myTest获得了空对象({})。

我认为我的问题出在plugin.xml js-module clobbers上..但是我的java似乎有问题。

有人可以帮帮我吗?谢谢你的进步。

0 个答案:

没有答案