JS and Android interaction

Source: Internet
Author: User

    
Local WebView notation
WebView = (webview) Findviewbyid (R.id.webview); Webview.loadurl ("http://192.168.1.103/index.html"); Webview.getsettings (). setjavascriptenabled (true// allow JS script to run Webview.setscrollbarstyle (view.scrollbars_inside_overlay); Webview.getsettings (). Setbuiltinzoomcontrols (true); Webview.getsettings (). setjavascriptenabled (true); Webview.addjavascriptinterface (new Jstojava (Mcontext), "Mobject"); // Add a mapping object to JS

JS Code


Body> <a>js Call local method </a> <script> function Funfromjs () { document.getElementById ("Helloweb"). Innerhtml= "Hellowebview,i ' m from JS"; } = document.getElementsByTagName (' a ') [0]; Atag.addeventlistener (' click ', Function () { // call Android Local method Myobj.fun1fromandroid ("Call Android native method Fun1fromandroid (String name)!! "); return false ; false ); </script> <p></p> <div id= "Helloweb" > </div></body>

Local and JS mapped objects:

 Public class javascriptobject {    Context mcontxt;      Public Javascriptobject (Context mcontxt) {        this. mcontxt = mcontxt;    }       Public void fun1fromandroid (String name) {        Toast.maketext (mcontxt, Name, Toast.length_long). Show ();      Public void fun2 (String name) {        "call fun2:" + name, Toast.length_short). Show ();    }}

JS and Android interaction

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.