[英]Using JSInterface to execute events on the WebView - Android
我有一个WebView
和一些调用某些Java代码的JavaScript
函数。 我已创建一个JSInterface
附加到此Webview
。 通过JSInterface
函数执行的Java代码最终会更改Webview
本身。
如果没有对WebView
本身的引用,该函数将无法更新WebView
。 我的JSInterface
实现必须包含对其所附接的WebView
的引用,这似乎很奇怪! 有一个更好的方法吗? 一些实现此目的的示例代码如下所示:
WebView
:
WebView m = findViewById(R.id.wv); m.getSettings().setJavaScriptEnabled(true); m.addJavascriptInterface(new MyJSInterface(), "Android");
JSInterface: public class MyJSInterface { @JavascriptInterface public void doSomething() { //need to update the webview here. //first get the Webview. MYWebView m = MainActivity.getWebView(); //this is my question. I need the webview that the JSInterface is attached to. m.loadUrl("http://www.google.com"); } }
为此,我需要像这样初始化JSInterface
: MyJSInterface myji = new MyJSInterface(webViewInstance);
然后做:
webViewInstance.addJavascriptInterface(myji, "Android");
我的问题:
1.这是正确的方法吗? 这不是圆形的,即JSInterface包含调用它的WebView对象的对象吗?
2.有更好的方法吗?
不,那不是完成的方式(您的MyJSInterface
没有仅适合初学者的constructor
...)。
这是我的问题。 我需要的
webview
,该JSInterface
连接到。
答案就是将其传递给构造函数(您已经找到了webview
)。
Q1。 这是正确的方法吗? 这不是圆形的,即JSInterface包含调用它的WebView对象的对象吗?
是的,这是正确的方法。 它不是循环的(或者递归,如果那是您的意思,除非您这样做了)。
Q2。 有更好的方法可以做到这一点吗?
这就是Google设计的方式。 有没有更好的方法,只有找到更好方法的人才能回答,即使那样也很主观 。
我的
JSInterface
实现必须包含对其所附接的WebView
的引用,这似乎很奇怪!
一点也不奇怪,它是对您需要的对象的引用。
这是记录的方式:
WebView webView = findViewById(R.id.wv);
webView.getSettings().setJavaScriptEnabled(true);
// Injects the supplied Java object into this WebView.
// The object is injected into the JavaScript context of the main frame,
// using the supplied name.
// This allows the Java object's public methods to be accessed from JavaScript.
webView.addJavascriptInterface(new JavaScriptInterface(this, webView), "Android");
//===========================================================================
public class JavaScriptInterface
{
Context mContext;
WebView mWebView;
// Instantiate the interface and set the context (constructor)
JavaScriptInterface(Context c, WebView webView)
{
mContext = c;
mWebView = webView;
}
//-----------------
// Show a web page from string
@JavascriptInterface
public void loadWebPage(String page)
{
mWebView.loadUrl("http://www.google.com");
}
//-----------------
// Show a toast from the web page
@JavascriptInterface
public void showToast(String toast)
{
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
//-----------------
//using Javascript to call the finish activity
@JavascriptInterface
public void closeMyActivity()
{
finish();
}
//-----------------
}//class JavaScriptInterface
//===========================================================================
这是一些使用上述代码的JavaScript:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<input type="button" value="Load Web Page" onClick="loadAndroidWebPage('http://www.google.com')" />
<input type="button" value="Close App" onClick="closeMyActivity()" />
<script type="text/javascript">
function showAndroidToast(toast)
{
Android.showToast(toast);
}
function loadAndroidWebPage(pageURL)
{
Android.loadWebPage(pageURL);
}
function AndroidClose()
{
Android.closeMyActivity();
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.