Interaction between webview and Javascript in Android

Source: Internet
Author: User

Android applicationsProgramYou can directly call Javascript in webviewCodeThe javascript code in webview can also call Android applications (that is, Java code). The following is an example:

1. JavaScript scripts call Android programs
To call addjavascriptinterface (OBJ, interfacename) in webview)
OBJ is the application that communicates with JavaScript, and interfacename is
Name, set as follows:

Webview = new webview (this );
Webview. getsettings (). setjavascriptenabled (true );
Webview. loadurl (getintent (). getcharsequenceextra ("url"). tostring ());
// Set the Interface Name of the Javascript script code to "android"
Webview. addjavascriptinterface (this, "android ");
In the HTML page called by webview, JS is as follows:
<SCRIPT type = "text/JavaScript">
Function OK (){
Android. js (document. Forms [0]. elements [0]. Value, document. Forms [0]. elements [1]. value );
}

Where is this android. js? That is in the Application
// Javascript script code can call the function JS () for processing
Public void JS (string action, string URI ){
...../
}

In this JS, the request sent by JavaScript is processed.

2) In the following example, when a webview webpage is entered, click the submit button to interact with Android applications.
Webview = new webview (this );
Webview. getsettings (). setjavascriptenabled (true );
Webview. setwebchromeclient (New mywebchromeclient ());
Webview. loadurl (getintent (). getcharsequenceextra ("url"). tostring ());

// The onjsalert () function receives the alert () warning message from the HTML webpage.
Public Boolean onjsalert (webview view, string URL, string message, jsresult result ){
If (message. Length ()! = 0 ){
Alertdialog. Builder = new alertdialog. Builder (jexample02.this );
Builder. settitle ("from JavaScript"). setmessage (Message). Show ();
Result. Cancel ();
Return true;
}
Return false;
}

JS events on the HTML page are:
<Input type = "button" value = "alert" onclick = "alert (document. Forms [0]. elements [0]. Value)">

Note: In the custom mywebchromeclient (), you can override onjsalert and
Rewrite onjsprompt and onjsconfirm. For more information, see
Http://618119.com/archives/2010/12/20/199.html

3) in the following example, the first image is displayed. Click a bit and then the second image is displayed.
In html js:
<Script language = "JavaScript">
Function changeimage02 (){
Document. getelementbyid ("image"). src = "navy02.jpg ";
}
Function changeimage01 (){
Document. getelementbyid ("image"). src = "navy01.jpg ";
}
</SCRIPT>
</Head>
<Body>
<A onclick = "window. Demo. onclick ()">
</a>
</Body>

When <a onclick = "window. Demo. onclick ()">
Then, call the Processing Section in the Android Application to view the program:
Webview. addjavascriptinterface (New jsinterface (), "Demo ");

Public final class jsinterface {
// The onclick () function that can be called by Javascript script code
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 ()");
}
}
});
}
}
In Android, webview. loadurl is used to call Js in the HTML page. Java-JavaScript mogujie.com

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.