【问题标题】:Using JSInterface to execute events on the WebView - Android使用 JSInterface 在 WebView 上执行事件 - Android
【发布时间】:2018-04-18 17:26:23
【问题描述】:

我有一个 WebView 和一些 JavaScript 函数,它们调用一些 Java 代码。我创建了一个JSInterface 附加到这个Webview。通过JSInterface 函数执行的Java 代码最终会更改Webview 本身。

如果不引用WebView 本身,该函数将无法更新WebView。我的JSInterface 实现必须包含对它所附加的WebView 的引用,这似乎很奇怪!有一个更好的方法吗?实现此目的的一些示例代码如下所示:

  1. WebView:

    WebView m = findViewById(R.id.wv);
    m.getSettings().setJavaScriptEnabled(true);
    m.addJavascriptInterface(new MyJSInterface(), "Android");
    
  2. 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"); } }

为了能够做到这一点,我需要像这样初始化JSInterfaceMyJSInterface myji = new MyJSInterface(webViewInstance); 然后执行:
webViewInstance.addJavascriptInterface(myji, "Android");

我的问题:
1. 这是正确的方法吗?这不是循环的,即 JSInterface 包含调用它的 WebView 对象的对象吗?
2. 有没有更好的方法来做到这一点?

【问题讨论】:

    标签: android webview


    【解决方案1】:

    不,这不是它的完成方式(您的MyJSInterface 没有constructor 只是为了初学者......)。

    这是我的问题。我需要webviewJSInterface 是 附在。

    答案是简单地将它传递给构造函数(你已经找到webview)。

    第一季度。这是正确的方法吗?这不是循环的,即包含调用它的 WebView 对象的对象的 JSInterface 吗?

    是的这是正确的方法。它是不是循环的(或者是递归的,如果这就是你的意思,除非你这样做)。

    第二季度。有没有更好的方法来做到这一点?

    这就是 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>
    

    【讨论】:

    • 这个我明白了。如果您需要从 JSInterface 中更改 WebView(一些设置等)怎么办?
    • @Sriram 您是否尝试过从界面更改webView 中的某些内容?你有错误吗?如果有problem,就只有problem!我没有看到一个!
    • 感谢您的回复。我已经更新了代码。我的问题是在 JSInterface 实现中是否可以引用它所附加的 WebView。希望能说明白吗?
    • @Sriram我更新了我的答案。是的,ok. 并且 clear 你想要什么,你认为它是 not 的事实表明你在某处遗漏了一个概念,或者放错了地方担心,但我不知道它是什么,除非你证明它。
    猜你喜欢
    • 2019-03-09
    • 2021-12-28
    • 2020-07-20
    • 1970-01-01
    • 2017-07-19
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多