android - Phonegap Cordova 2.2 plugin development, cannot communicate between Java and JavaScript -
i have been trying work past few hours now. have cordova 2.2.
i created new package called com.tester.newvideouri
.
i have class called newvideouri
in package following content
package com.tester.newvideouri; import org.apache.cordova.api.callbackcontext; import org.apache.cordova.api.cordovaplugin; import org.json.jsonarray; import org.json.jsonexception; /** * class echoes string called 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 , display it"); if (message != null && message.length() > 0) { callbackcontext.success(message); } else { callbackcontext.error("expected 1 non-empty string argument."); } } }
in config.xml
added following line:
<plugin name="echo" value="com.tester.newvideouri.newvideouri" />
in javascript
file have following:
function onload() { document.addeventlistener("deviceready", ondeviceready, false); } window.ondeviceready = function() { window.echo = function(str, callback) { alert('started'); cordova.exec(callback, function(err) { callback('nothing echo.'); }, "echo", "echo", [ str ]); alert('the end'); }; window.echo("echome", function(echovalue) { alert(echovalue == "echome"); // should alert true. }); }
nothing happens when run code. can please tell me doing wrong?
write "echome" code function this:
function echome(){ window.echo("echome", function(echovalue){ alert(echovalue == "echome"); }); }
and call function in html code this:
<a href="javascript:echome();">click here</a>
try run it. working me. :)
Comments
Post a Comment