我一直试图让这个工作过去几个小时了。我有cordova 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.
});
}
运行代码时没有任何反应。谁能告诉我我做错了什么?
答案 0 :(得分:0)
将您的“echome”代码写入如下函数:
function echome(){
window.echo("echome", function(echoValue){
alert(echoValue == "echome");
});
}
并在html代码中调用此函数,如下所示:
<a href="javascript:echome();">Click here</a>
尝试运行它。它对我有用。 :)
答案 1 :(得分:0)
可能你的问题是科尔多瓦未能“勾结”起来。你确实在你的网页上添加了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
}
答案 2 :(得分: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 ");})
}
我希望你能通过这个例子理解。
答案 3 :(得分: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 ");})
}
我希望你能通过这个例子理解。