String param1 = "Hello";
String param2 = "World";
String script = "yourJavaScriptFunction('" + param1 + "', '" + param2 + "');";
webView.loadUrl("javascript:" + "yourJavaScriptFunction('" + param1 + "','" + param2 + "');");
备注:如果参数里包'字符串,javascript会解析失败,需要先转义一下: String param2 = param2 .replace("'", "\\'");
然后JavaScript接收到参数之后再还原一下:
<script>
function yourJavaScriptFunction(param1, param2) {
param2 = param2.replace(/\\'/g, "'");
console.log("Received param1: " + param1);
console.log("Received param2: " + param2);
}
</script>