如何升级javascript以使用phonegap 2.3自定义插件

How to upgrade javascript to utilize phonegap 2.3 custom plugin?

本文关键字:自定义 插件 phonegap 何升级 javascript      更新时间:2023-09-26

我已经构建了一个使用Phonegap/Cordova 1.6.1的自定义插件TCPIPCommPlugin。一切都很好,回调/插件结果工作得很好。然而,我需要把它升级到Cordova 2.3。原因是我们现在开始在Win8和iOS/Android上开发游戏。

除了

,我在javascript中有以下代码。

var TCPComm = function() {
};
TCPComm.prototype.Open = function(Cmd,successCallback, failureCallback) {
 return PhoneGap.exec(    successCallback,    //Success callback from the plugin
                          failureCallback,     //Error callback from the plugin
                          'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                          'Open',              //Tell plugin, which action we want to perform
                          [Cmd]);        //Passing list of args to the plugin
};

这段代码继续向插件调用大约10-12个不同的函数,然后以…

结束。
PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin("TCPComm", new TCPComm());
});

在javascript内部,实际的函数调用看起来是这样的。

window.plugins.TCPComm.Open(g_IpAddr, OpenOK,OpenFail);

另外这是JAVA插件的样子

@Override
    public PluginResult execute(String action, JSONArray data, String callbackId) {
           PluginResult result = null;
        try {
            Actions currentAction = Actions.valueOf(action.toUpperCase());
            JSONObject Resp = new JSONObject();
            String RespStr;
            switch(currentAction){
            case OPEN:
            {
                       //do work
                    }catch (JSONException jsonEx) { 
        System.out.println(jsonEx.toString());
        result = new PluginResult(Status.JSON_EXCEPTION);
    }
    return result;}

这在Cordova 1.6.1中工作得很好。然而,对于Cordova 2.x.x,情况就不一样了。现在,说了这么多,做了这么多,我已经仔细阅读了网络,试图找到一种转换JAVA的方法。我得出了如下结论。

public boolean execute(String action, JSONArray data, CallbackContext, CallbackContext) {

    PluginResult result = null;
    try {
        Actions currentAction = Actions.valueOf(action.toUpperCase());
        JSONObject Resp = new JSONObject();
        String RespStr;
        switch(currentAction){
        case OPEN:
        {
                       //do work
                     }catch (JSONException jsonEx) {    
        System.out.println(jsonEx.toString());
        result = new PluginResult(Status.JSON_EXCEPTION);
    }
    return true;
}

这似乎与更新后的代码匹配。我没能找到的是一种更新JAVASCRIPT调用的方法,使这个插件与更新的CORDOVA一起工作。

任何帮助/指出正确的方向将非常感激!

我使用了以下文档,但没有成功。http://docs.phonegap.com/en/2.3.0/guide_plugin-development_index.md.html插件% 20发展% 20指南

https://github.com/apache/cordova-android/tree/master/framework/src/org/apache/cordova

谢谢你的回复Simon。在此期间,我用以下代码替换了我的javascript。我需要恢复到之前的状态吗?

cordova.define("cordova/plugin/TCPIPCommPlugin", function(require, exports, module){
    var exec = require('cordova/exec');
    var TCPComm = function() {};
//  var TCPCommError = function(code, message) {
//        this.code = code || null;
//        this.message = message || '';
//    };
    TCPComm.prototype.Open = function(success,fail) {
          return cordova.exec(              successCallback,    //Success callback from the plugin
                                  failureCallback,     //Error callback from the plugin
                                  'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                                  'Open',              //Tell plugin, which action we want to perform
                                  [Cmd]);
    };
    var TCPIPCommPlugin = new TCPComm();
    module.exports = TCPIPCommPlugin;
});

更新#2 -修复一些错误

我回到旧的javascript和替换它,所以它现在看起来像这样。

var TCPComm = function() {}; 

TCPComm.prototype.Open = function(Cmd, successCallback,failureCallback) {
      return cordova.exec(              successCallback,    //Success callback from the plugin
                              failureCallback,     //Error callback from the plugin
                              'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                              'Open',              //Tell plugin, which action we want to perform
                              [Cmd]);
};

我还将构造函数替换为.

    if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.TCPComm) {
    window.plugins.TCPComm = new TCPComm();
}

现在当我在Chrome中运行它时,(UI调试),我可以看到插件内置了函数内的所有适当函数。

Java很好,我忘了在论坛代码上包含返回。哎呀。

现在我试着调用函数,就像我一直有,我得到一个对象#没有方法'exec'在第一个JAVASCRIPT调用关闭/打开..

    window.plugins.TCPComm.Close("", Dummy, Dummy);
window.plugins.TCPComm.Open(g_IpAddr, OpenOK,OpenFail);

我在Java中设置了断点,让我知道插件何时成功调用了Java,但仍然没有运气。

还有其他想法吗?再次感谢您的建议

替换PhoneGap。执行以下代码:

var TCPComm = function() {
}; 
TCPComm.prototype.Open = function(Cmd,successCallback, failureCallback) {
 return cordova.exec(    successCallback,    //Success callback from the plugin
                      failureCallback,     //Error callback from the plugin
                      'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                      'Open',              //Tell plugin, which action we want to perform
                      [Cmd]);        //Passing list of args to the plugin
};

add构造方法已弃用,因此:

if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.videoPlayer) {
    window.plugins.TCPComm = new TCPComm();
}

对于你的Java代码,你是大部分的方式,但你需要返回结果:

public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
    PluginResult result = null;
    try {
        Actions currentAction = Actions.valueOf(action.toUpperCase());
        JSONObject Resp = new JSONObject();
        String RespStr;
        switch(currentAction){
        case OPEN:
            //do work
            this.callbackContext.sendPluginResult(
                new PluginResult(PluginResult.Status.OK, results));
    } catch (JSONException jsonEx) {    
        System.out.println(jsonEx.toString());
        result = new PluginResult(Status.JSON_EXCEPTION);
    }
    return true;
}

这应该能让你升级。