[英]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.