插件 – Cordova.exec函数不运行本机函数

我尝试在IBM工作灯中制作一个cordova插件.

使用Javascript

HelloWorld = {     
  sayHello: function (success, fail, resultType) { 
      Cordova.exec( 
          success, 
          fail, 
          "HelloWorld", 
          "HelloWorld", 
           [resultType]
      );
   }
};

function callFunction() {
    HelloWorld.sayHello(basarili, basarisiz, "sinan");
}

Java的:

package com.Cordova1;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;

import android.util.Log;
public class HelloWorld extends CordovaPlugin {
    public boolean execute(String arg0, JSONArray arg1, String arg2) {
        Log.d("HelloPlugin", "Hello, this is a native function called from PhoneGap/Cordova!"); 
        return true;
    }
}

当我调用callFunction时,我看到失败函数有效.此外,我在日志窗口中看不到任何HelloPlugin消息.
我能做什么 ?

模块09_3示例中的ApacheCordovaPlugin确实使用了弃用的Plugin类而不是CordovaPlugin.我在模块09_3中重写了HelloWorldPlugin类,以消除不推荐使用的Cordova Plugin API用法.样本工作正常.

package com.AndroidApacheCordovaPlugin;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

public class HelloWorldPlugin extends CordovaPlugin {

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

        if (action.equals("sayHello")) {
            String responseText = "Hello world";
            try {
                responseText += ", " + arguments.getString(0);
                callbackContext.success(responseText);
                return true;
            } catch (JSONException e) {
                callbackContext.error(e.getMessage());
            }
        } else {
            callbackContext.error("Invalid action: " + action);
            return false;
        }
        return false;
    }
}
相关文章
相关标签/搜索