公式リンク: http://developer.android.com/reference/android/webkit/WebView.html
class JsObject {
@JavascriptInterface
public String toString() { return "injectedObject"; }
}
webView.addJavascriptInterface(new JsObject(), "injectedObject");
webView.loadData("", "text/html", null);
webView.loadUrl("javascript:alert(injectedObject.toString())");
------------------------------- MUST API>17
私の調整、HTML、jsobj.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hello TEST JS</title>
</head>
<body style="background: white; font-family: Helvetica">
<script type="text/javascript">
document.write(AndroidFunction.show());
AndroidFunction.save("abc");
</script>
</html>
アンドロイド:
web.addJavascriptInterface(new JsObject(), "AndroidFunction");
...
class JsObject{
@JavascriptInterface
public void save(String action){
L.d("action:"+action);
}
public String show(){
return "Hello Android";
}
}