The Android WebView uses Java to invoke the JavaScript method and get the return value _android

Source: Internet
Author: User
Tags html page stub

Open a Web page with WebView on the Android tablet, call the JavaScript method inside, and pass the argument to each other.
Online examples are very little ah, basically can not get the return value, paste a final debugging of their own code as follows:
Java:

Copy Code code as follows:

protected void OnCreate (Bundle savedinstancestate)
{
........
x = (WebView) This.findviewbyid (r.id.webview_viewtable);
X.setscrollbarstyle (View.scrollbars_inside_overlay);
X.getsettings (). Setbuiltinzoomcontrols (True);
X.getsettings (). Setjavascriptenabled (True);
X.addjavascriptinterface (New Jstojava (), "stub"); Jstojava is the inner class, and the code is in the back. Stub is the interface name.

X.loadurl ("http://192.168.1.1/init.html");//This sentence is loaded into an HTML page. But because there is a delay in directly load a page, it is best to use the following sentence:
X.loaddatawithbaseurl ("", Data, "text/html", "UTF-8", ""); In this sentence, data is the content of init.html. is the code. Just use FileInputStream to get it right.

X.loadurl ("Javascript:setvaluesjson (' Hello World ')"); Hello world is a parameter that is passed to JavaScript. Setvaluesjson is a way of JavaScript on the page. If there are double quotes in the arguments passed, use the following notation:
String url = "Javascript:setvaluesjson (\" + jsonstring + "\") ";
X.loadurl (URL);
.........
}//oncreate End

Write an inner class again:

Copy Code code as follows:

Private Class Jstojava
{
public void Jsmethod (String paramfromjs)
{
LOG.I ("CDH", PARAMFROMJS);
System.out.println ("JS return result" + PARAMFROMJS);//process returned results
}
}

JavaScript code:

Copy Code code as follows:

<script type= "Text/javascript" >
function Setvaluesjson (param)
{
alert (param);//param is the value of Java, the "Hello World".
var result = "Send back to Java string";
Window.stub.jsMethod (result);//////////////////////////////////
}
</script>


Get.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.