Listen to javascript calls of a WebView

烂漫一生 提交于 2021-01-27 20:00:35

问题


I has the following HTML

<div>
    <input type="submit" onClick="anyJSFunction('Test')">
</div>

And I open it on a WebView like:

        web = new WebView(this);

        WebSettings settings = web.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setSupportMultipleWindows(false);
        settings.setSupportZoom(false);
        settings.setPluginsEnabled(true);
        settings.setDomStorageEnabled(true);

        web.setVerticalScrollBarEnabled(false);
        web.setHorizontalScrollBarEnabled(false);

        web.loadUrl("file:///sdcard/test.html");

Now I want to listen on Java when 'anyJSFunction' is called and also be able to get it arguments.

How can it be done?


回答1:


There's a decent example in the documentation for WebView. Look for "Binding JavaScript code to Android code."

You'll build a class as an interface from JavaScript to Java/Android. You create an instance and pass it to the WebView. Then the JavaScript code can reference the class instance by name.



来源:https://stackoverflow.com/questions/8326809/listen-to-javascript-calls-of-a-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!