调用Java函数从JavaScript过的Andr​​oid的WebView [英] Call Java function from JavaScript over Android WebView

查看:291
本文介绍了调用Java函数从JavaScript过的Andr​​oid的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做出一些Java code同步调用在我的Andr​​oid应用程序。

I want to make a synchronous call to some Java code in my Android app.

我使用这个解决方案: http://stackoverflow.com/a/3338656

I am using this solution: http://stackoverflow.com/a/3338656

我的Java code:

My Java code:

final class MyWebChromeClient extends WebChromeClient {
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            Log.d("LogTag", message);
            result.confirm();
            return true;
        }
    }

我的JavaScript code:

My JavaScript code:

<html>
<script>
function java_request(){
    alert('test');
}
</script>
<body>
<h2>Welcome</h2>
<div id="area"></div>
<form>
<input type="button" value="java_call" onclick="java_request()">
</form>
</body>
</html>

当我轻触 java_call 按钮,该按钮转到pressed状态。我可以看到测试在控制台日志。一切正常,直到这里。

When I tap on the java_call button, the button goes to the pressed state. I can see 'test' in the console log. Everything is normal until here.

的问题是,按钮永远不会恢复到正常状态。它停留在pressed状态。也许JavaScript执行坏了还是怎么了?

The problem is, the button never gets back to its normal state. It stays in the pressed state. Maybe the JavaScript execution is broken or something?

为什么按钮再也不会恢复到正常状态?

Why does the button never return to its normal state?

推荐答案

我不认为这是去执行java code的JavaScript的最佳解决方案。在这里看到:

I don't think this is the best solution to get the javascript to execute java code. See here:

如果要公开本机code到HTML是通过javascript调用,做在你的Web视图声明如下:

If you want to expose native code to the HTML to be callable via javascript, do the following around your web view declaration:

JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");

声明类 JavaScriptInterface

public class JavaScriptInterface {
    private Activity activity;

    public JavaScriptInterface(Activity activiy) {
        this.activity = activiy;
    }

    public void startVideo(String videoAddress){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(videoAddress), "video/3gpp"); 
        activity.startActivity(intent);
    }
}

我声明一个单一的功能,用于播放视频,但是你可以做任何你想要的。

I am declaring a single function for playing a video, but you can do whatever you want.

最后,调用这个在的WebView 通过简单的JavaScript调用的内容:

Finally you call this in the WebView contents via simple javascript call:

<video width="320" height="240" controls="controls" poster='poster.gif'
       onclick="window.JSInterface.startVideo('file:///sdcard/test.3gp');" >
   Your browser does not support the video tag.
</video>

这个例子是取自我的另外一个答案,关于播放视频,而应解释就够了。

The example is taken from another answer of mine, about playing videos, but should be explaining enough.

修改作为每@ CedricSoubrie的评论:如果应用程序的目标版本设置为17或更高版本,你需要添加注释 @JavascriptInterface 每个方法上面要导出到Web视图。

EDIT As per @CedricSoubrie's comment: if the target version of the application is set to 17 or higher you need to add annotation @JavascriptInterface above each method you want to export to the web view.

这篇关于调用Java函数从JavaScript过的Andr​​oid的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆