Android Share notes (5) Android and JS interaction

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.