繁体   English   中英

Phonegap Cordova 2.2插件开发,无法在Java和JavaScript之间进行通信

[英]Phonegap Cordova 2.2 plugin development, cannot communicate between Java and JavaScript

我一直在尝试使它在过去的几个小时内正常工作。 我有科尔多瓦2.2。

我创建了一个名为com.tester.newvideouri的新包。

我在此包中有一个名为newVideoUri的类,其中包含以下内容

package com.tester.newvideouri;

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

/**
 * This class echoes a string called from JavaScript.
 */
public class newVideoUri extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("echo")) {
            String message = args.getString(0); 
            this.echo(message, callbackContext);
            return true;
        }
        return false;
    }

    private void echo(String message, CallbackContext callbackContext) {
        System.out.println("success here and display it");
        if (message != null && message.length() > 0) { 
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }
}

在我的config.xml ,添加了以下行:

    <plugin name="Echo" value="com.tester.newvideouri.newVideoUri" />

在我的javascript文件中,我有以下内容:

    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
    window.onDeviceReady = function() {         
        window.echo = function(str, callback) {
            alert('Started');
            cordova.exec(callback, function(err) {
                callback('Nothing to echo.');
            }, "Echo", "echo", [ str ]);
            alert('The END');
        };          
        window.echo("echome", function(echoValue) {
            alert(echoValue == "echome"); // should alert true.
        });
    }

当我运行代码时,什么都没有发生。 谁能告诉我我在做什么错?

将您的“ echome”代码写入如下函数:

function echome(){
    window.echo("echome", function(echoValue){
    alert(echoValue == "echome");
    });
}

并在html代码中调用此函数,如下所示:

<a href="javascript:echome();">Click here</a>

尝试运行它。 它为我工作。 :)

此示例中的代码是正确的: http : //docs.phonegap.com/en/2.6.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android

您的问题可能是Cordova无法“挂断”。 您确实将cordova.android.js添加到了网页上,对吗?

如果是这样,请确保已实际调用就绪设备:

window.onDeviceReady = function() {
  alert('ready to work'); 
   // if you do not see this alert nothing in cordova will work as 
   // it means communication was not started

}

您需要在JavaScript文件中输入以下内容:

cordova.define("cordova/plugin/sampleplugin", function(require, exports, module) {
  var exec = require('cordova/exec');
  var SamplePlugin = function() {};

  SamplePlugin.prototype.authenticate = function(loginId,plainTextPassword,successCallback,failureCallback) {
        return exec(successCallback, failureCallback, 'SamplePlugin', 'authenticate', [loginId,plainTextPassword]);

  }
        var sampleplugin = new SamplePlugin();
        module.exports = sampleplugin;
});

这里SamplePlugin是我的插件的名称。

同样在您的html文件中,编写以下代码:

var sampleplugin = cordova.require("cordova/plugin/sampleplugin");
           function  callrestfulweb()
{
               var loginId= document.getElementById("loginId").value;
               var plainTextPassword = document.getElementById("plainTextPassword").value;
               sampleplugin.authenticate(loginId,plainTextPassword,function(){alert("login successful");},function(){alert("Invalid username or password ");})
       }

希望您能通过这个例子理解。

您需要在JavaScript文件中输入以下内容:

cordova.define("cordova/plugin/sampleplugin", function(require, exports, module) {
  var exec = require('cordova/exec');
  var SamplePlugin = function() {};

  SamplePlugin.prototype.authenticate = function(loginId,plainTextPassword,successCallback,failureCallback) {
        return exec(successCallback, failureCallback, 'SamplePlugin', 'authenticate', [loginId,plainTextPassword]);

  }
        var sampleplugin = new SamplePlugin();
        module.exports = sampleplugin;
});

这里SamplePlugin是我的名字

同样在您的html文件中,编写此代码

var sampleplugin = cordova.require("cordova/plugin/sampleplugin");
           function  callrestfulweb()
{
               var loginId= document.getElementById("loginId").value;
               var plainTextPassword = document.getElementById("plainTextPassword").value;
               sampleplugin.authenticate(loginId,plainTextPassword,function(){alert("login successful");},function(){alert("Invalid username or password ");})
       }

希望您能通过这个例子理解。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM