3

I want to run JavaScript function on my android app,this is how i create the webview:

m_FullJSWebView = new WebView(m_VideoView.getContext());
m_FullJSWebView.loadData(htmltmp, "text/html", "UTF-8");
m_FullJSWebView.getSettings().setJavaScriptEnabled(true);
m_FullJSWebView.addJavascriptInterface(new JavaScriptInterface(m_VideoView.getContext()), "MyAndroid");
m_FullJSWebView.loadUrl("javascript:getValue()");

This is the html:

<html>
  <head>
    <script type="text/javascript">
    function getValue(){
       //return value to Android 
       var val= 50;
       MyAndroid.receiveValueFromJs(val);
    }
    </script>
    <title></title>
  </head>
  <body >
    <form name="ipForm" id="ipForm">
      UserName : <input type="text" name="userName">
      <button type="button" onclick="getValue();">Submit</button>
    </form>
  </body>
</html>

And this is the JavascriptInterface:

public class JavaScriptInterface {
        Context mContext;
        JavaScriptInterface(Context c) {
            mContext = c;
        }
        //add other interface methods to be called from JavaScript

        public void receiveValueFromJs(String str) {
            //do something useful with str
              Toast.makeText(mContext, "Received Value from JS: " + str,Toast.LENGTH_SHORT).show();
        }
}

After i run it on my device the receiveValueFromJs function won't called.Any idea what is the problem?

2
  • did you check that For applications targeted to API level JELLY_BEAN_MR1 and above, only public methods that are annotated with JavascriptInterface can be accessed from JavaScript. Commented Jul 16, 2013 at 9:07
  • probably not relevant for now, but you'll have to annotate your method for the next releases. Commented Jul 16, 2013 at 9:22

1 Answer 1

1

From the doc:

Note that injected objects will not appear in JavaScript until the page is next (re)loaded.

Which means that you must change your methods order like that:

m_FullJSWebView = new WebView(m_VideoView.getContext());
m_FullJSWebView.addJavascriptInterface(new JavaScriptInterface(m_VideoView.getContext()), "MyAndroid");
m_FullJSWebView.loadData(htmltmp, "text/html", "UTF-8");
m_FullJSWebView.getSettings().setJavaScriptEnabled(true);
m_FullJSWebView.loadUrl("javascript:getValue()");

edit (2nd issue)

the name parameter in the addJavascriptInterface is name of the java object in the javascript. it is the object on which to call the methods from the javascript.

Therefore, your call should be:

m_FullJSWebView.loadUrl("javascript:MyAndroid.getValue()");
Sign up to request clarification or add additional context in comments.

6 Comments

I change to this and the receiveValueFromJs still won't called
It's still won't call the receiveValueFromJs function, there is any other way to run JS and return a value from it
probably because JavaScript interacts with Java object on a private, background thread of this WebView. means that you cannot show a Toast from there.
The receiveValueFromJs won't call at all,i put breakpoint there
what device are you running on (what version ?)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.