【问题标题】:JavescriptInterface and thread not returning to main threadJavascriptInterface 和线程未返回主线程
【发布时间】:2017-08-21 08:10:48
【问题描述】:

我正在为 android 开发一个应用程序,其中一些内容通过 WebView 显示。

我有一个这样的 Javascript 接口。

WebAppInterface(Context c, Response response) {
        activity = (Activity) c;
        delegate = response;
        context = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void getResponse(final String operation, final String response, final String type) {
        final Handler mainHandler = new Handler(context.getMainLooper());
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                delegate.didGetResponse(operation, response, type);
                Log.d("Success: ", "Im still here"); <-- why am I not back in main thread? 
            }
        });    
    }

    public interface Response {
        public void didGetResponse(String operation, String response, String type);
    }

我的活动实现了 didGetResponse 并有一个开关,它执行代码并在需要时更新 webview,但仅在 webview 中的第一个函数上,因此在页面设置后 webview 不再响应“点击”

我在想是因为线程还在 javascriptinterface 中?

但是,原生按钮仍然可以更新 web 视图。但是我希望能够从webview中新加载的url调用js函数

【问题讨论】:

  • 好的,当我单击新加载的 url 中的元素时,我将其缩小为“未捕获的错误:找不到方法”

标签: javascript android multithreading webview


【解决方案1】:

好的,所以我找到了答案,原来我正在调用一个带有少量参数的 javascript 函数。所以我的解决方案奏效了,问题是我的 js 函数在这个页面上只有 2 个参数而不是 3 个。这与 swift 不同,它只使用 2 个参数。

【讨论】:

  • +1 我也收到“未捕获的错误:找不到方法”,这是因为在 Javascript 端我用 1 个参数调用函数,但本机端暴露了一个期望 2论据。
猜你喜欢
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多