(v) Interaction between WebView and JavaScript in Android

Source: Internet
Author: User

In Android applications, the JavaScript code in WebView can be called directly, while the JavaScript code in WebView can also be called to the Android application (that is, the JAVA part of the code). The following examples illustrate:

1. JAVASCRIPT Script calls Android program
To call Addjavascriptinterface (Obj,interfacename) in WebView, OBJ is an application that communicates with JavaScript and InterfaceName is provided to JavaScript The name of the call, set as follows:

<span style= "FONT-SIZE:14PX;" >webview WebView = new WebView (this); Webview.getsettings (). Setjavascriptenabled (True); Webview.loadurl (Getintent (). Getcharsequenceextra ("url"). toString ());//Set JavaScript script code interface name is "Android" Webview.addjavascriptinterface (This, "Android");</span>
where WebView is called in the HTML page, JS is as follows:

<span style= "FONT-SIZE:14PX;" ><script type= "Text/javascript" >function Ok () {android.js (Document.forms[0].elements[0].value, Document.forms[0].elements[1].value);} </script></span>
and where is this android.js? It's in the application.
<span style= "FONT-SIZE:14PX;" >//javascript script code can invoke the function JS () to handle public void JS (string action, string uri) {...../}</span>
This JS is the processing of JAVASCRIPT sent over the request.

2, the following example, when the WebView Web page input, click the Submit button, will interact with the ANDROID application

<span style= "FONT-SIZE:14PX;" >webview WebView = new WebView (this); Webview.getsettings (). Setjavascriptenabled (True); Webview.setwebchromeclient (New Mywebchromeclient ()); Webview.loadurl (Getintent (). Getcharsequenceextra ("url"). toString ()); the//onjsalert () function receives alert () from the HTML Web page Warning Message public boolean onjsalert (WebView view, string URL, string message, jsresult result) {if (Message.length ()! = 0) {Alert Dialog.builder Builder = new Alertdialog.builder (jexample02.this); Builder.settitle ("From JavaScript"). Setmessage ( Message). Show (); Result.cancel (); return true;} return false;} </span>
the JS event in the HTML page is:
<span style= "FONT-SIZE:14PX;" ><input type= "button" value= "alert" onclick= "alert (document.forms[0].elements[0].value)" ></span>
In particular, in the custom Mywebchromeclient ()), you can rewrite the onjsprompt,onjsconfirm, and so on, in addition to rewriting Onjsalert.
3, the following example, first display the first picture, click a point, then display the 2nd picture
in HTML JS:

<span style= "FONT-SIZE:14PX;" ><script language= "JavaScript" >function ChangeImage02 () {document.getElementById ("image"). src= " Navy02.jpg ";} function changeImage01 () {document.getElementById ("image"). src= "Navy01.jpg";} </script>

When point <a onclick= "Window.demo.onClick ()" >, Call the Processing section of the ANDROID app to see the program

<span style= "FONT-SIZE:14PX;" >webview.addjavascriptinterface (New Jsinterface (), "demo");p Ublic final class Jsinterface {//javascript The script code can call the function onclick () to handle public void OnClick () {handler.post (new Runnable () {public void run () {if (flag = = 0) {flag = 1; Webview.loadurl ("Javascript:changeimage02 ()");} else {flag = 0; Webview.loadurl ("javascript:changeimage01 ()");}});}} </span>

you can see that in ANDROID, the Webview.loadurl to invoke JS in the HTML page.

In fact, in the first article, it has been exposed to the WebView and JS call each other, generally introduced to this bar.




(v) Interaction between WebView and JavaScript in Android

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.