Java and JS interaction in WebView

Source: Internet
Author: User

WebView provides a powerful feature for displaying Web pages in Android apps. It is also the foundation of the vigorous development of Hybird app at present. As a very important component of the Android system, it provides two powerful capabilities: parsing, layout and rendering of HTML, and the interpretation and execution of JavaScript. The Hybird app is made up of the native+h5,native part of the Java language implementation, while JavaScript is an essential part of H5. As a result, Java and JavaScript are called to each other! Here's a basic example of calling each other!

Adding WebView components to the 1.Native layout

 1  <  webview  2  = "@+id/wv_contacts"   Android:layout_ Below  = "@id/tv_title"  4   Android:layout_width  = "Match_parent"   5   android:layout_height   >  6  </ webview  >  

2. Initialize WebView, set allow JavaScript and load page

1   Private void Initwebview () {2         mwebview = (WebView) Findviewbyid (r.id.wv_contacts); 3         WebSettings settings = mwebview.getsettings (); 4         settings.setjavascriptenabled (true); 5         Mwebview.loadurl ("file:///android_asset/constacts.html"); 6     }

3.java calls JavaScript (Mwebview.loadurl ("Javascript:method (param)");)
First define the JavaScript function:

1 functionShowData (constactsdata) {2             varHtml= "";3             varUllist = document.getElementById ("Contacts_list");4             varConstactsjsondata =eval (constactsdata);5              for(vari = 0; i < constactsjsondata.length; i++){6HTML + = "<li Onclick=callphone (\" "+ Constactsjsondata[i].number +" \ ") >" + constactsjsondata[i].name + "</li&gt ;";7             }8ullist.innerhtml =html;9}

Then in Java call JavaScript, put in the onpagefinished callback call is to ensure that the call JS, JS has been fully loaded complete

1Mwebview.setwebviewclient (Newwebviewclient () {2 @Override3              Public Booleanshouldoverrideurlloading (WebView view, String URL) {4 view.loadurl (URL);5                 return true;6             }7 8 @Override9              Public voidonpagefinished (WebView view, String URL) {Ten                 Super. onpagefinished (view, URL); One showcontactsinfo (); A             } -         }); -  the Private voidShowcontactsinfo () { -String info =jsinterface.readcontacts (); -         Mwebview.loadurl ("javascript:showdata (" + info + ")"); -}

4. In JS call Java (mwebview.addjavascriptinterface (New Javascriptinterface (), "interface");). Call the Java native method in JS, as shown in the code. You need to convert the object of the method you want to call into an alias. Pass this alias through to JavaScript and then access the native method through the alias in JavaScript.

First add an alias

1 mwebview.addjavascriptinterface (New Jsinterface (This.getapplicationcontext ()), "Jsinterface");
Then define the native method

1  Public void Callphone (String number) {2         New Intent (Intent.action_call, Uri.parse ("Tel:" + number ); 3         intent.setflags (intent.flag_activity_new_task); 4         mcontext.startactivity (intent); 5     }

Finally, the call is made through the alias in JavaScript.
1 function callphone (number) { 2 jsinterface.callphone (number); 3 }

If you have completed the most basic Java and JavaScript communication features in the Hybird app!

Java and JS interaction in WebView

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.