8

I want to get the value from my JS function on my Java Android

Sample,

function getStringToMyAndroid(stringFromAndroid){
   var myJsString = "Hello World" + ;
   return myJsString;
}

Now I want calling function getStringToMyAndroid("This string from android") and get the returning myJsString on my Android, so I can use myJsString later on my Android;

I know I can use

WebView.loadUrl("javascript:getStringToMyAndroid('This string from android')")

to call the JS function but I want to get the string or value from JS function

NOTE: My android running on minimum SDK Android 3.0 honeycomb

Cœur
  • 37,241
  • 25
  • 195
  • 267
ilovebali
  • 513
  • 2
  • 7
  • 20

1 Answers1

16

For API Level < 19 there are only workarounds of either using a JavascriptInterface (my preferred method, below) or else hijacking the OnJsAlert method and using the alert() dialog instead. That then means you can't use the alert() function for its intended purpose.

View:

WebView.addJavascriptInterface(new JsInterface(), "AndroidApp");
WebView.loadUrl("javascript:doStringToMyAndroid('This string from android')")

JsInterface:

public class JsInterface() {
    @JavascriptInterface
    void receiveString(String value) {
        // String received from WebView
        Log.d("MyApp", value);
    }
}

Javascript:

function doStringToMyAndroid(stringFromAndroid){
   var myJsString = "Hello World" + ;
   // Call the JavascriptInterface instead of returning the value
   window.AndroidApp.receiveString(myJsString);
}

But on API Level 19+, we now have the evaluateJavascript method:

WebView.evaluateJavascript("(function() { return getStringToMyAndroid('" + myJsString + "'); })();", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        Log.d("LogName", s); // Returns the value from the function
    }
});
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • Hi, thanks for your code and explanation, I have one question is doStringToMyAndroid is function from JS?? WebView.loadUrl("doStringToMyAndroid('This string from android')"), thanks I will try now :) – ilovebali Aug 01 '14 at 07:41
  • @ilovebali Sorry, yes that's a Javascript function. Replacement for your `getStringToMyAndroid` - I've updated the code to make that clearer – CodingIntrigue Aug 01 '14 at 07:43
  • I tried your code and no luck yet :(, I tried also use the last code that make my app crash :), what I do is I created new java class from JsInterface and called it from my webView `WebView.addJavascriptInterface(new JsInterface(), "AndroidApp"); WebView.loadUrl("javascript:doStringToMyAndroid('This string from android')")`, need advise, thanks :) – ilovebali Aug 01 '14 at 08:03
  • Have you set [WebView.getSettings().setJavaScriptEnabled(true)](http://developer.android.com/reference/android/webkit/WebSettings.html#setJavaScriptEnabled(boolean)) – CodingIntrigue Aug 01 '14 at 08:07
  • On your class `public class JsInterface() { @JavascriptInterface void receiveString(String value) { // String received from WebView Log.d("MyApp", value); } }` you have **"()"** after class name, on mine is I remove it, because I got Error:(9, 25) error: '{' expected, then I remove the **"()"** from class name, I got no error but the code is not working, thanks :) – ilovebali Aug 01 '14 at 08:27
  • Now the code is working perfectly, It was my bad, I put wrong webView so the code can not get the right webView, thanks for your help :D – ilovebali Aug 01 '14 at 08:41
  • @RGraham, I am using API 16 in AVD, So I used your answer for API level <19. In my LogCat there is no message prints as it was in JSInterface class [Log.d("MyApp", value);].. Is there any thing I need to do? –  May 11 '16 at 11:36