Working with Javascript from Android

from the CommonsWare Community archives

At November 25, 2021, 8:36am, Raptor asked:

Hello,

I need to implement some webpage that has some javascript in it in a webview in order for it to take some images using the phone’s camera. To do this, I must instantiate (or access, not sure yet) an object from the javascript code and call a method on it.

However, it’s not clear to me on how to do so. I have a guide at Interaction between WebView and JS. In the documentation received from the solution that I will be using I have this:

Native application will create an WebView by loading index.html file Solution.

After the page load is completed, a global scripting object will be available, accessible at window.solution in the scription context of container WebView. The native application is responsible to call startCapture metod on solution object in order to start camera capture. This method promise a callback promise object that will be called upon document capture, and accepts a string representing the document type it needs to capture.

As an example of using this, I have:

const solution = new Solution();
solution 
    .startCapture(
        "DocumentType",
        "https://solution.com/",
        "Bearer 123"
    )
    .then((res) => console.log(res));

I am not… very sure what I’m supposed to do with all this. From what I saw, I can call JS code using webView.evaluateJavascript("javascript:startCapture()") but:

  1. How do I instantiate that solution object? If I don’t need to instantiate it, what does “a global scripting object will be available, accessible at window.solution in the scription context of container WebView” mean? :))
  2. How do I actually call that method with the necessary arguments? And how do I call it on the solution object?
  3. Do I need to call it in the onPageFinished() method in order to have access to the actual javascript code?
  4. Do I expect a response from calling the method in the associated lambda of the evaluateJavascript() method?

Here’s how I approached it thus far:

webView.settings.javaScriptEnabled = true
webView.loadUrl("file:///android_asset/index.html")
webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                webView.evaluateJavascript("javascript:startCapture()") {
                    Log.d("Solution", it)
                }
            }
        }

Thanks!


At November 25, 2021, 2:39pm, mmurphy replied:

I am not a strong JavaScript developer and cannot advise you on this one.

Same as above.

If by “the actual javascript code” you mean JavaScript loaded in the Web page that you are loading via loadUrl(), then yes.

Whatever your script evaluates to will be what you get in the evaluateJavascript() lambda. With your script being javascript:startCapture(), then yes, I would expect the lambda to contain the stringified version of whatever startCapture() returns. Whether that is something useful, I cannot say.