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