jojosch
jojosch

Reputation: 51

How can I pass data to Javascript in a WebView using a Javascript interface in Java code?

I am trying to pass a variable from my Android Java code to Javascript in a WebView using a Javascript Interface, but the alert says "undefined".

This is part of the Java:

public class WebAppInterface {
    Context mContext;

    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public int getValue() {
        return 5;
    }
 }

And the Javascript:

function getValue() {
    Android.getValue();
}

var value = getValue();
alert(value);

Note that my Interface is called "Android". What am I doing wrong?

Upvotes: 1

Views: 2724

Answers (1)

Christopher Scott
Christopher Scott

Reputation: 2399

Try adding the 'return' keyword to your getValue function:

function getValue() {
  return Android.getValue();
}

alert(getValue());

Upvotes: 3

Related Questions