Skip to content Skip to sidebar Skip to footer

Android Returns Methods Must Be Called On Same Thread Error

I am trying to call a WebView method from a method which is called by javascript in the webview. Webview then should return value that is used within the method. html event -> j

Solution 1:

As you said, when you call methods from JS to the JavaBridge and then to your class from a non-ui Thread, you have to use listeners/callbacks. When you have done your loadUrl, call you listener to pass the desired String url.

In this Example, I've defined an an inner interface with the onGetWebViewUrl listener. I've registered the listener by implementing the interface in the same class, so I can call it easily.

For example :

publicclassWebViewProxy implement WebViewListener {

    privateWebView mWebView;

    ...

    @JavascriptInterfacepublicvoidonJsStuff(){
        webView.post(newRunnable() {
             publicvoidrun() {
                mWebView.loadUrl("javascript:" + s + ";");
                WebViewProxy.this.onGetWebViewUrl(mWebView.getUrl());
            }
        });
    }

    @OverridepublicvoidonGetWebViewUrl(String url){
        // call method 2 with this url
    }

    publicinterfaceWebViewListener {

        voidonGetWebViewUrl(String url);
    }
}

Post a Comment for "Android Returns Methods Must Be Called On Same Thread Error"