1

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?

1 Answer 1

3

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

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

alert(getValue());
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.