简体   繁体   English

从android webview触发JavascriptInterface

[英]triggering JavascriptInterface from a android webview

I followed an instruction to trigger the JavascriptInterface from a webview, but it isnt triggered in my case. 我按照一条指令从webview中触发了JavascriptInterface,但在我的情况下它没有被触发。

I have an class QuickTextViewer with following: 我有一个类QuickTextViewer,其中包括:

    public class QuickTextViewer {
        private WebView webView;
    ...
    ...
        public QuickTextViewer(){
           webView = dialog.findViewById(R.id.mywebview);

           webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                 view.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
                super.onPageFinished(view, url);
            }
        }
@JavascriptInterface
    public void resize(final float height) {
        System.out.print(height);
    }

I also added the following to proguard-rules.pro (actually public only for testing) 我还在proguard-rules.pro中添加了以下内容(实际上仅用于测试)

-keepclassmembers class fqcn.of.javascript.interface.for.webview {
   public *;
}

In my case onPageFinished is triggered but resize() not ! 在我的情况下onPageFinished被触发但是resize()没有! Any suggestions/help ? 有什么建议/帮助吗?

Found the problem now. 现在发现了问题。 Just had to add: 只需添加:

webView.getSettings().setJavaScriptEnabled(true);

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

相关问题 Android WebView JavascriptInterface属性 - Android WebView JavascriptInterface Property 从文本框中获取值-Android WebView和JavascriptInterface问题 - Getting values from textbox - Android WebView and JavascriptInterface issue 如何从webView,Android中的@JavascriptInterface方法调用Javascript? - How to call Javascript from @JavascriptInterface method in webView, Android? 无法识别 JavaScriptInterface 方法(Android webview) - JavaScriptInterface methods not recognizable (Android webview) 如何为 android webview 实现 @JavascriptInterface - How to implement @JavascriptInterface for android webview 如何从Android WebView JavascriptInterface调用admob非页内广告 - How to call admob Interstitial ad from android webview JavascriptInterface 使用 @JavascriptInterface 从 webview 调用活动 - calling activity from webview with @JavascriptInterface 来自JavascriptInterface的android startActivity - android startActivity from JavascriptInterface 使用JavascriptInterface从javascript触发Toast - eclipse using JavascriptInterface triggering toast from javascript Android中的速度更快:使用loadUrl将数据发送到WebView或使用JavascriptInterface从WebView检索数据? - What's faster in Android: sending data to a WebView with loadUrl or retrieving it from the WebView with a JavascriptInterface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM