android - Cordova call JavaScript function from Java without webview -
how can call javascript function java. example have sayhello function in www/js/app.js. need write method in java call sayhello , give result using cordova.
sayhello: function(name) { return "hello "+name; }
try this
//method exec javascript private static synchronized void executejavascript(final string strjs, cordovawebview mywebview) { runnable jsloader = new runnable() { public void run() { mywebview.loadurl("javascript:" + strjs); } }; try { method post = mywebview.getclass().getmethod("post",runnable.class); post.invoke(mywebview,jsloader); } catch(exception e) { ((activity)(mywebview.getcontext())).runonuithread(jsloader); } }
assuming have class
public class mycordovaplugin extends cordovaplugin
you have method
@override public void initialize (cordovainterface cordova, cordovawebview webview) { //and call method executejavascript("sayhello('bear')", webview); }
Comments
Post a Comment