简体   繁体   English

如何在android webview中注入javascript界面​​?

[英]How to inject javascript interface in android webview?

I am loading a page in webview in android. 我正在android的webview中加载页面。 The page has an element(button) with the following code, NB:- It is a third party website and I have no control over the code shown here :- 该页面具有一个带有以下代码的元素(按钮),注意:-这是第三方网站,我无法控制此处显示的代码:-

    <i class="icon close cardClose" role="button" title="Close recently linked screen" tabindex="0"></i>

I want to add a javascript interface as in the below method :- 我想按以下方法添加JavaScript接口:-

private class myWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            view.addJavascriptInterface(new Object()
            {
                @JavascriptInterface
                public void performClick() throws Exception
                {
                    Toast.makeText(Activity.this, "Login clicked", Toast.LENGTH_LONG).show();
                }
            }, "login");
            return true;
        }
    }


mWebView.setWebViewClient(new myWebViewClient());

But the problem is that webview source code says :- 但是问题是,webview源代码显示:-

* @param object the Java object to inject into this WebView's JavaScript
     *               context. Null values are ignored.
     * @param name the name used to expose the object in JavaScript
     */
    public void addJavascriptInterface(Object object, String name) {
        checkThread();
        mProvider.addJavascriptInterface(object, name);
    }

The button doesnot seem to have an attribute named "name". 该按钮似乎没有名为“ name”的属性。 Please throw some insight into the same if some one has knowledge. 如果有人有知识,请对它们进行一些了解。

I recommed you to use it this way 我建议你用这种方式

view.addJavascriptInterface(new JavaScriptInterfaceNew(getActivity(), this), "Android_Normal_Test");

and here JavaScriptInterfaceNew is your class name and Android_Normal_Test will be your object when you need to pass some event from webview to your activity class 当您需要将某些事件从webview传递到activity class时,这里的JavaScriptInterfaceNew是您的类名,而Android_Normal_Test将是您的对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM