Phonegap Java call Javascript Function -
i making hello world type phonegap application android, in call javascript function native android code set text of 1 of paragraphs in app.
i have javascript function define in app's html, , want call activity once app loads up. function sets text of paragraph in app. cannot work.
here java activity code:
public class myphonegapactivity extends droidgap { public droidgap c; public loadinfo info; @suppresslint("setjavascriptenabled") @javascriptinterface @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); super.init(); c = this; appview.getsettings().setjavascriptenabled(true); appview.getsettings().setjavascriptcanopenwindowsautomatically(true); appview.loadurl("file:///android_asset/www/index.html"); appview.setwebviewclient(new webviewclient() { public void onpagefinished(webview view, string url) { myphonegapactivity.this.runonuithread(new runnable() { public void run() { c.sendjavascript("javascript:getmasstimes()"); } }); } }); } }
and here javascript , html code:
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title>ionic seed app</title> <link href="lib/css/ionic.css" rel="stylesheet"> <link href="css/app.css" rel="stylesheet"> <link href="lib/css/mycss.css" rel="stylesheet"> <script src="lib/js/ionic.js"></script> <script src="lib/js/angular/angular.js"></script> <script src="lib/js/angular/angular-animate.js"></script> <script src="lib/js/angular/angular-sanitize.js"></script> <script src="lib/js/angular-ui/angular-ui-router.js"></script> <script src="lib/js/ionic-angular.js"></script> <script src="js/app.js"></script> <script src="js/services.js"></script> <script src="js/controllers.js"></script> <script src="http://code.jquery.com/jquery-2.1.0.js"></script> </head> <body ng-app="myapp"> <script type="text/javascript"> function getmasstimes() { document.getelementbyid("masstimes").textcontent = "this text"; } </script> <div> <header-bar title="saint david king" class="bar bar-header bar-energized"> <h1 class="title">saint david king</h1> </header-bar> <br> <content scroll="true" has-header="true" width="100%" height="100%" id="content"> <img alt="saint david king" src="file:///android_asset/www/img/saintdavidthekingbackground2.png" width="100%" /> <br> <p id="masstimes"> </p> </content> </div> </body> </html>
is there doing wrong? have seen these sites:
resolved - call javascript function java
how call javascript function java code
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/1o9pgfmyrd8
http://www.jumpbyte.com/2012/phonegap-native-to-javascript/
javascript calls android using phonegap
how call javascript function android using phonegap plugin
and have tried have on them, can't work.
any appreciated!
replace c.sendjavascript("javascript:getmasstimes()");
mybrowser.loadurl("javascript:getmasstimes()");
.
worked problem.
if want pass string activity javascript use
string msgtosend = "hello world!"; mybrowser.loadurl("javascript:callfromactivity(\""+msgtosend+"\")");
Comments
Post a Comment