WebView interacting with JavaScript (Android):
WebView and JavaScript interaction is two-way data transfer, 1.H5 Web page JS function call native function 2.Native function call JS function, the specific implementation of the following example mainly:
1.) Add network permissions in Mainfest.xml
<uses-permission android:name= "Android.permission.INTERNET"/>
2.) WebView Open support JavaScript
Mwebview.getsettings (). Setjavascriptenabled (True);
3.) Simple H5 Web implementation, the main implementation of Actionfromnative (), Actionfromnativewithparam (String str), placed under the assets file
4.) native implementation and JS Interactive function: Actionfromjs (), Actionfromjswithparam () public class Mainactivity extends Activity {private WebView mwebview; Private TextView Logtextview; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Mwebview = (WebView) Findviewbyid (R.id.webview); Enable JavaScript mwebview.getsettings (). Setjavascriptenabled (True); Load HTML Mwebview.loadurl ("file:///android_asset/wx.html") from the assets directory; Mwebview.addjavascriptinterface (This, "WX"); Logtextview = (TextView) Findviewbyid (R.id.text); Button button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (New Button.onclicklistener () {public void OnClick (View v) {//parameterless call Mwebview.loadurl ("javascript:actionfromnative ()"); Pass parameters Call Mwebview.loadurl ("Javascript:actionfromnativewithparam (" + "' come from Native '" + ")"); } }); } @android. Webkit.javascriptinterface public void Actionfromjs () {Runonuithread (new Runnable () {@Ove Rride public void Run () {Toast.maketext (mainactivity.this, "JS called native function", toast.length_short) . Show (); String text = Logtextview.gettext () + "\njs called the native function"; Logtextview.settext (text); } }); } @android. Webkit.javascriptinterface public void Actionfromjswithparam (final String str) {Runonuithread (new Runnable () {@Override public void run () {Toast.maketext (mainactivity.this, "JS called n ative function Pass parameter: "+ str, toast.length_short)." Show (); String text = Logtextview.gettext () + "\njs called the native function pass parameter:" + str; Logtextview.settext (text); } }); }}Mwebview.addjavascriptinterface (This, "WX"), which is equivalent to adding a JS callback interface, and then giving this an alias, I'm the name of WX (haha). @android. Webkit.javascriptinterface in order to solve the addjavascriptinterface loophole, only after 4.2.
5.) Layout File implementation<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <webview android:id= "@+id/webview" android:layout_width= "Match_parent " android:layout_height=" 0DP " android:layout_weight=" 1 "/> <textview android:id=" @+id/text " android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= ""/> <button android:id= "@+id/button" android:layout_width= "match_parent" android:layout_ height= "Wrap_content" android:text= "native call js function"/></linearlayout>
6.) Simple explanation of the code(1.) JS (HTML) access to the Android (Java) side code is implemented through the Jsobj object, calling functions in the Jsobj object, such as: Window.jsObj.actionFromJs (), The jsobj here is the alias of the add interface in native
(2) Android (Java) Access JS (HTML) side code is implemented through the Loadurl function, access to the format such as: Mwebview.loadurl ("javascript:actionfromnative ()");
Demo Run:
Objective-c interacting with JavaScript (iOS)Http://www.cocoachina.com/ios/20160127/15105.html
The interaction between JS and native