WebView and JavaScript interactions 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
where obj is an application that communicates with JavaScript, InterfaceName is provided for JavaScript calls.
Name, set as follows:

WebView WebView = new WebView (this);
Webview.getsettings (). Setjavascriptenabled (True);
Webview.loadurl (Getintent (). Getcharsequenceextra ("url"). toString ());
The interface name for setting JavaScript script code is "Android"
Webview.addjavascriptinterface (This, "Android");
Where WebView is called in the HTML page, JS is as follows:
<script type= "Text/javascript" >
function ok () {
Android.js (Document.forms[0].elements[0].value, Document.forms[0].elements[1].value);
}

And where is this android.js? It's in the application.
JavaScript script code can call the function JS () processing
public void JS (string action, string uri) {
...../
}

This JS is the processing of JavaScript sent over the request.

2) The following example, when the WebView Web page is entered, the Click submit button will interact with the Android app
WebView WebView = new WebView (this);
Webview.getsettings (). Setjavascriptenabled (True);
Webview.setwebchromeclient (New Mywebchromeclient ());
Webview.loadurl (Getintent (). Getcharsequenceextra ("url"). toString ());

The Onjsalert () function receives alert () warning messages from the HTML Web page
public boolean Onjsalert (WebView view, string URL, string message, jsresult result) {
if (Message.length ()! = 0) {
Alertdialog.builder Builder = new Alertdialog.builder (jexample02.this);
Builder.settitle ("from JavaScript"). Setmessage (Message). Show ();
Result.cancel ();
return true;
}
return false;
}

The JS event in the HTML page is:
<input type= "button" value= "alert" onclick= "alert (document.forms[0].elements[0].value)" >

Specifically, in the Custom Mywebchromeclient ()), in addition to rewriting onjsalert, you can
Rewrite onjsprompt,onjsconfirm, etc., can refer to
Http://618119.com/archives/2010/12/20/199.html

3) The following example shows the first picture, click a point, then display the 2nd picture
In HTML JS:
<script language= "JavaScript" >
function ChangeImage02 () {
document.getElementById ("image"). src= "Navy02.jpg";
}
function ChangeImage01 () {
document.getElementById ("image"). src= "Navy01.jpg";
}
</script>
<body>
<a onclick= "Window.demo.onClick ()" >
</a>
</body>

When point <a onclick= "Window.demo.onClick ()" >
After calling the processing section of the Android application, look at the program:
Webview.addjavascriptinterface (New Jsinterface (), "demo");

Public final class Jsinterface {
JavaScript script code can invoke the function onclick () processing
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 ()");
}
}
});
}
}
Can see that in Android, through the Webview.loadurl to call the HTML page JS

WebView and JavaScript interactions 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.