How do I pass return values from a javascript function to android?
Asked Answered
L

1

12

I want to let my android app call a function written in javascript and expect a return value from that.

I understand that WebView.loadUrl works asynchronously, so what I am doing now is to let javascript notify my android app when it is done and pass in the return value by calling a java function using javascriptinterface.

I wonder if there are better ways of doing this and whether anyone has noticed any message loss between javascript and android.

Li answered 10/3, 2011 at 19:3 Comment(1)
This is a duplicate of How to get return value from javascript in webview of android?Chemulpo
D
31

I just got your problem.

Have a JS function like this.

function androidResponse() {
   window.cpjs.sendToAndroid("I am being sent to Android.");
}

Set up Android (Java).

Have a final class like this

final class IJavascriptHandler {
   IJavascriptHandler() {
   }

   // This annotation is required in Jelly Bean and later:
   @JavascriptInterface
   public void sendToAndroid(String text) {
      // this is called from JS with passed value
      Toast t = Toast.makeText(getApplicationContext(), text, 2000);
      t.show();
   }
}

Then on your WebView load have.

webView.addJavascriptInterface(new IJavascriptHandler(), "cpjs");

Call JS function

webView.loadUrl("javascript:androidResponse();void(0)");

UPDATED


Also I had a very bad time experiencing problems while passing hundreds of lines of string to JS from Java and I have subsequent post on StackOverflow with no good answers but finally resolved it knowing problme was of special characters inside string so take of special characters when you use string passing to and fro.

Passing Data From Javascript To Android WebView

HTML String Inside Nested String

HTML TextArea Characters Limit Inside Android WebView

Disentangle answered 10/3, 2011 at 19:13 Comment(7)
Edited as I understand the problem now.Disentangle
Thanks for your response. I have been doing a similar thing here. I have found that the calls from java to javascript are asynchronous, but the ones backwards are synchronous. Is that right? Also, have you noticed any message loss from javascript to java in your experiment?Li
This is obvious the first one is asynchronous and the other one is synchronous but I have not read the documentation. My job was more toward sending huge HTML data from Java to JS and I did find loss of data due to special characters in the long string of HTML and I also find that we can't just pass huge HTML string using webView.loadUrl("..."); so I got a workaround for this.Disentangle
What characters provoke problems? How huge is ”huge”?Enrico
#5002917Disentangle
@Neutralizer you answer is useful, but I have a little problem, my webview has several local html's to show, one of them must to read and load json data from (my) remote server and the html document (in local asset folder) doesn't display until all data are read. It seems that the js function androidResponse (I followed your answer here) is not called until the html has loaded completely in the webview. If I click by second time the button that call androidResponse then the TOAST message is now shown. Can you figure out the issue?Dugong
I know this question is already 4 years old, but I am wondering did someone find a way to transfer huge data from Android to JS?Tanagra

© 2022 - 2024 — McMap. All rights reserved.