1

I have to call a javascript method from my webview, passing parameters from native. And the result I have to get back in native. In api 19+ I'm using

mWebView.evaluateJavascript(JAVASCRIPT_NATIVE_REFERENCE_NAME, new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {

                    }
                })

And its working fine. But evaluateJavascript is not supported in API <=18 . Can someone please show a sample how it can be done in API 18 below. I already saw some SO answers

passing data to javascript in webview before android level 17 (4.2.2)

How to get return value from javascript in webview of android?

as shown in second question , can we call any method present in javascript like view.loadUrl("javascript:alert(functionThatReturnsSomething())"); ?? Where we get the callback in this case. It would be helpful if someone could show a sample. Thanks in advance!!

  • "can we call any method present in javascript like view.loadUrl("javascript:alert(functionThatReturnsSomething())"); ?" -- so long as you do not need the result, sure. "Where we get the callback in this case" -- you don't. There is no callback. At best, you use `addJavascriptInterface()` to inject some Java object into the JavaScript namespace, then have the function call a method on that object. This has security issues. – CommonsWare Oct 25 '17 at 12:28
  • I need the result back in java code , and based on that I have to call the next javascript method. In this case what can I do? Can u share some code? –  Oct 25 '17 at 12:44
  • "In this case what can I do?" -- I would recommend that you set your `minSdkVersion` to 19 and use `evaluateJavascript()`. – CommonsWare Oct 25 '17 at 12:49
  • app has to support from 18 :( –  Oct 25 '17 at 13:08

0 Answers0