Call the Java method in JS:
(1) First to provide the interface for JS
webview webview; @Overrideprotected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview (R.layout.activity_ Main); webview = (WebView) findviewbyid (R.id.webview); webview.getsettings (). Setjavascriptenabled (True); webview.setscrollbarstyle ( View.scrollbars_outside_overlay); webview.loadurl (" file:///android_asset/js/ index.html "); webview.addjavascriptinterface (New androidtoastforjs ( Mainactivity.this), "demo");} public class androidtoastforjs { private context mcontext; public androidtoastforjs (Context context) { this.mcontext = context; } @ JavascrIptinterface public string gettitle () { return "This is titl"; } @ Javascriptinterface public string getcontent () { return "this is content ,this is contentthis is Contentthis is content "; } @JavascriptInterface public string getstatement () { return "This is statement"; }}
Pass this interface object to JS (WebKit)
Webview.addjavascriptinterface (New Androidtoastforjs (Mainactivity.this), "demo");
So, now JS know a "demo" interface class for him to use
(2) Call the Java method in JS:
Finish calling Java method in JS!
Figure:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7D/8D/wKioL1bqjmOwfWM-AACMsuuTtDk445.png "title=" 339673723708565260.png "alt=" Wkiol1bqjmowfwm-aacmsuuttdk445.png "/>
This article from "Sharing is the best memory" blog, declined reprint!
Android Share notes (5) Android and JS interaction