我正在尝试从 java android 代码调用 javascript 函数。我试图查看有关此问题的所有先前问题,但似乎没有任何效果。有什么建议吗?谢谢!
javascript code:
<html>
<head>
<script language="javascript">
function doSomething() {
alert('hey!!!!!!!');
}
</script>
</head>
<body>
ilana
</body>
</html>
java code (from an activity on create)
final WebView webview=(WebView)findViewById(R.id.myWebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");
webview.loadUrl("file:///android_asset/www/index.html");
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("javascript:doSomething()");
我更新(但仍然无法正常工作)的代码是:javascript:
<html>
<head>
<script type="text/javascript">
function doSomething(){
alert("hello");
}
</script>
</head>
<body>
hello hello hello!
</body>
</html>
java:
final WebView webview=(WebView)findViewById(R.id.myWebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/www/index.html");
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:doSomething()");
}
});