0

在过去的几个小时里,我一直在努力让它工作。我有科尔多瓦 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.
        });
    }

当我运行代码时没有任何反应。谁能告诉我我做错了什么?

4

4 回答 4

0

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

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

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

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

尝试运行它。它对我有用。:)

于 2013-05-10T06:52:24.010 回答
0

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

您的问题可能是科尔多瓦未能“连接”起来。您确实将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

}
于 2013-07-16T18:57:51.937 回答
0

你需要在你的 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 ");})
       }

我希望你能通过这个例子来理解。

于 2013-09-25T11:13:50.507 回答
0

您需要在您的 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 ");})
       }

我希望你能通过这个例子来理解。

于 2013-09-25T11:08:41.500 回答