添加JavaScript接口浏览器 [英] Adding JavaScript Interface to a Browser

查看:97
本文介绍了添加JavaScript接口浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能一个JavaScript接口添加到Android浏览器可以被添加到的WebView组件作为的在这个演示说明。我的具体使用情况下,只需要使用Javascript - > Android的,这样我可以回到previous活动发

Is it possible to add a JavaScript interface to the Android Browser the same way one can be added to the WebView Component as illustrated in this demo. My particular use case only needs JavaScript -> android so that I can send it back to the previous activity.

推荐答案

您可以通过调用在你的WebView方法和功能的JavaScript的URL,例如:

You can invoke methods and functions in your webview by using javascript url's, e.g.

webview.loadUrl("javascript:somemethod()");

您会,当然,需要启用javascript对你的WebView:

You will, of course, need to enable javascript on your webview:

webview.getSettings().setJavaScriptEnabled(true);

这是从Java到JavaScript的。如果你想从JavaScript调用Java code / Android的API的使用addJavascriptInterface()

This is from java to javascript. If you want to invoke java code / android API's from javascript, use addJavascriptInterface()

webview.addJavascriptInterface(new MyJSJavaBridge(), "api");

所有这些例子中所示的网址您发布为好。

All of this is shown in the example url you posted as well.

这篇关于添加JavaScript接口浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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