Android WebView read HTML and JS

Source: Internet
Author: User

Before the way through the bridge, there are security issues, Android 4.2 when the repair of this vulnerability, security requirements are relatively high, recommended not to use, please refer to

This blog of mine: http://blog.csdn.net/com360/article/details/38492007


WebView's support for JavaScript is also strong, Google a Java and JavaScript intermodulation example
Throughout the Eclipse ADT Engineering example, here's an emphasis on code: Java Code    public class webviewdemo extends activity  {       private WebView mWebView;        private handler mhandler = new handler ();           public void oncreate (bundle icicle)  {            super.oncreate (icicle);            Setcontentview (R.layout.webviewdemo);           mwebview  =  (WebView)  findviewbyid (r.id.webview);            websettings websettings = mwebview.getsettings ();            websettings.setjavascriptenabled (true);            mwebvIew.addjavascriptinterface (New object ()  {                public void clickonandroid ()  {                    mhandler.post (New Runnable ( )  {                        public void run ()  {                             mwebview.loadurl ("Javascript:wave ()");                        }                   &NBSP});                }           },  "Demo");            mwebview.loadurl ("file:///android_asset/demo.html");        }  }  

The focus here is the Addjavascriptinterface (object Obj,string InterfaceName) method, which binds a Java object to a JavaScript object, The JavaScript object name is InterfaceName, and the scope is global. After the webview is initialized, it is possible to access the bound Java object directly through Javascript:window.demo in the WebView loaded page. To see how it's called in HTML: HTML code   

This allows you to invoke the Clickonandroid () method of the Java object in JavaScript, and the Wave () method is an example of Java calling JavaScript.

Here are a few more points of knowledge:

1 to allow WebView to load the Assets,android SDK from the APK file provides a schema, prefixed with "file:///android_asset/". WebView encounters such a schema, it goes to the assets directory in the current package to find content. As the "file:///android_asset/demo.html" above

2 Java objects and methods to be bound in the Addjavascriptinterface method to run another thread, it cannot be run in the thread that constructs him, which is also the purpose of using handler.

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.