使用 JSInterface 在 WebView 上执行事件 - Android [英] Using JSInterface to execute events on the WebView - Android

查看:30
本文介绍了使用 JSInterface 在 WebView 上执行事件 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WebView and some JavaScript functions that call some Java code. I have created a JSInterface that is attached to this Webview. The Java code that is executed via the JSInterface function ultimately makes changes to the Webview itself.

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

Without a reference to the WebView itself, the function cannot update the WebView. It seems odd that my JSInterface implementation must contain a reference to the WebView to which it is attached! Is there a better way to do this? Some sample code to achieve this looks like so:

  1. WebView:

WebView m = findViewById(R.id.wv);
m.getSettings().setJavaScriptEnabled(true);
m.addJavascriptInterface(new MyJSInterface(), "Android");

  • JS接口:公共类 MyJSInterface {@JavascriptInterface公共无效doSomething(){//这里需要更新webview.//首先获取Webview.MYWebView m = MainActivity.getWebView();//这是我的问题.我需要 JSInterface 附加到的 webview.m.loadUrl("http://www.google.com");}}

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

    To be able to do this, I need to initialize the JSInterface like so: MyJSInterface myji = new MyJSInterface(webViewInstance); and then do :
    webViewInstance.addJavascriptInterface(myji, "Android");

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

    My question:
    1. Is this the right way to do this? Is this not circular, i.e., the JSInterface containing an object of the WebView object that calls it?
    2. Are there better ways to do this?

    推荐答案

    不,这不是它完成的方式(您的 MyJSInterface 没有 constructor 仅用于初学者……).

    No that is not the way it is done (your MyJSInterface does not have a constructor just for starters...) .

    这是我的问题.我需要 JSInterfacewebview附在.

    this is my question. I need the webview that the JSInterface is attached to.

    答案只是将其传递给构造函数(您已经找到webview).

    The answer is simply pass it in to the constructor(you already found the webview).

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

    Q1. Is this the right way to do this? Is this not circular, i.e., the JSInterface containing an object of the WebView object that calls it?

    是的这是执行此操作的正确方法.它不是循环(或者递归,如果这就是你的意思,除非你让这样).

    Yes this is the right way to do this. It is NOT circular (or recursive if that's what you mean, unless you make it so).

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

    Q2. Are there better ways to do this?

    这就是 Google 设计的方式.有没有更好的方法,只能由找到更好方法的人来回答,即使这样也是主观.

    This is the way Google designed it to be. Is there a better way, can only be answered by someone who found a better way, and even then it is subjective.

    我的 JSInterface 实现必须包含一个引用它所附加的 WebView

    It seems odd that my JSInterface implementation must contain a reference to the WebView to which it is attached!

    一点也不奇怪,它是对您需要的对象的引用.

    Not odd at all, it is a reference to an object you need.

    这里是记录的方式:

    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:

    Here is some JavaScript that uses the above code:

    <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 上执行事件 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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