Android WebView Development JS Call Java code example

Source: Internet
Author: User

1.webView settings

Webview.getsettings (). Setjavascriptenabled (true);//Set Support JS
Webview.addjavascriptinterface (New Jsoperation (), "client");//The Java class that sets the JS call


2. Declaring JS to invoke the Java class

Class Jsoperation {

test method
@JavascriptInterface// This sign must be written or it will be a problem.
public void Test (String param) {
Toast.maketext (Mainactivity.this,param,toast.length_short). Show ();

}
}

3. Calling methods in the Java class in JS

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Testing </title>
<script type= "Text/javascript" >
function test (param) {
client.test (param); calling methods in Java
}

</script>
<body>
<br/>
<button onclick= "Test (' JS call Java method ')" >test</button>
</body>

Android WebView Development JS Call Java code example

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.