Webview calls Javascript

Source: Internet
Author: User

A Preliminary Study on webview shows that webview is powerful ,. I heard that webview has a strong support for Javascript, and it is still difficult to find some examples from the Internet. Finally, I found an example of interdebugging between Java and JavaScript from Google's hometown, the chin "when" fell to the ground, too strong! This works too?

The entire eclipse ADT project example is provided. The code is analyzed here:

Java code

Public class webviewdemo extends activity {<br/> private webview mwebview; <br/> private handler mhandler = new handler (); </P> <p> Public void oncreate (bundle icicle) {<br/> super. oncreate (icicle); <br/> setcontentview (R. layout. webviewdemo); <br/> mwebview = (webview) findviewbyid (R. id. webview); <br/> websettings = mwebview. getsettings (); <br/> websettings. setjavascriptenabled (true); <br/> mwebview. addjavascriptinterface (new object () {<br/> Public void clickonandroid () {<br/> mhandler. post (New runnable () {<br/> Public void run () {<br/> mwebview. loadurl ("javascript: Wave ()"); <br/>}< br/>}); <br/>}< br/>}, "Demo "); <br/> mwebview. loadurl ("file: // android_asset/demo.html"); <br/>}< br/>} 

 

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 = mwebview. getsettings (); websettings. setjavascriptenabled (true); mwebview. addjavascriptinterface (new object () {public void clickonandroid () {mhandler. post (New runnable () {public void run () {mwebview. loadurl ("javascript: Wave ()") ;}}}, "Demo"); mwebview. loadurl ("file: // android_asset/demo.html ");}} 

Here the focus is on the addjavascriptinterface (Object OBJ, string interfacename) method. This method binds a Java object to a JavaScript Object. The JavaScript Object Name Is interfacename and the scope is global. After webview initialization, you can directly access the bound Java object through javascript: window. demo on the page loaded by webview. Let's see how html is called:

HTML code

<HTML> <br/> <MCE: script language = "JavaScript"> <! -- </P> <p> function wave () {<br/> document. getelementbyid ("droid "). src = "android_waving.png"; <br/>}</P> <p> // --> </MCE: SCRIPT> <br/> <body> <br/> <a onclick = "window. demo. clickonandroid () "> <br/> <br/> click me! <Br/> </a> <br/> </body> <br/> </ptml> 

<HTML> <MCE: script language = "JavaScript"> <! -- <Br/> function wave () {document. getelementbyid ("droid "). src = "android_waving.png" ;}< br/> // --> </MCE: SCRIPT> <body> <a onclick = "window. demo. clickonandroid () "> <br> click me! </A> </body> </ptml> 

In this way, the clickonandroid () method of the Java object can be called in Javascript. The wave () method is an example of JavaScript calling in Java.

Here are some knowledge points:

1) To allow webview to load assets from the APK file, the android SDK provides a schema prefixed with "file: // android_asset /". When webview encounters such a schema, it finds the content in the Assets Directory of the current package. Above "file: // android_asset/demo.html"

2) the Java object and method to be bound in the addjavascriptinterface method must run in another thread and cannot be run in the Construction thread. This is also the purpose of using handler.

 

Related Article

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.