簡體   English   中英

如何升級javascript以利用phonegap 2.3自定義插件?

[英]How to upgrade javascript to utilize phonegap 2.3 custom plugin?

我已經構建了一個名為TCPIPCommPlugin的自定義插件,它使用Phonegap / Cordova 1.6.1。 一切都很棒,回調/插件結果很好。 但是我需要將它升級到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.xx並沒有那么多。現在說完所有這些,我已經仔細閱讀了網絡試圖找到轉換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#Plugin%20Development%20Guide

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

UPDATE

謝謝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很好,我忘了在論壇代碼中包含返回。 哎呦。

現在我嘗試調用函數,就像我一直有的一樣,在第一次JAVASCRIPT調用Close / Open時,我得到一個Object#沒有方法'exec'。

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

我在執行的Java中設置了斷點,讓我知道插件成功調用Java時仍然沒有運氣。

還有其他想法嗎? 再次感謝您的見解。

用以下代碼替換PhoneGap.exec:

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;
}

這應該讓你升級。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM