Android via WebView loading JS Web code between the two interactions
public class mainactivity extends activity {private webview webview;@ Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main);webview = (WebView) findViewById ( R.ID.WEBVIEW1); Websettings websettings = webview.getsettings (); websettings.setjavascriptenabled (true);// Support Javascrip must have Websettings.setdefaulttextencodingname ("Utf-8"); Webview.addjavascriptinterface (this, "test") );//corresponding to JS in the test.xxx, through the tag to call the method in Android Webview.setwebchromeclient (New webchromeclient () {});//To add this sentence, Otherwise, some models will not be able to pop up Windows Webview.loadurl ("http://192.168.3.4:8080/WebViewTest/test.html");//Start a Web page with Tomcat, of course, the local line// Webview.loadurl ("file:///android_asset/test.html");//put it in assets directory to}/** * js call the method * @param msg */@JavascriptInterfacepublic void helloandroid (string msg) { toast.maketext (Mainactivity.thiS, msg, toast.length_short). Show ();} /** * invoke JS in method * @param v */public void call (view v) {Webview.post (New runnable () { @Override public void run () { webview.loadurl ("Javascript:calljs ()");              LOG.V ("Zd", "CallJS"); } });}}
XML layout, only
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <linearlayout android: Layout_width= "Match_parent" android:layout_height= "match_parent " android:orientation=" > "vertical" <LinearLayout android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "1" > <button android:layout_width= "Wrap_ Content " android:layout_height= "Wrap_content" android:onclick= "Call" android:text= "Call js" /> </LinearLayout> <LinearLayout android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "2" > <WebView android:id= "@+id/webview1" android:layout_width= "Match_parent" android:layout_height= "Match_parent" /> </linearlayout> </linearlayout></ Linearlayout>
JS code, which refers to the method in Android using the test previously declared
<! DOCTYPE html>
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/89/45/wKiom1gOBOHQYJIkAABtYvzFTDU910.png-wh_500x0-wm_3 -wmp_4-s_541465495.png "title=" Qq20161024205543.png "alt=" wkiom1gobohqyjikaabtyvzftdu910.png-wh_50 "/>650" this.width=650; "Src=" http://s4.51cto.com/wyfs02/M01/89/43/wKioL1gOBUayTdQOAABEjPE4hLw068.png-wh_500x0-wm_3-wmp _4-s_2103317989.png "title=" Qq20161024205723.png "alt=" Wkiol1gobuaytdqoaabejpe4hlw068.png-wh_50 "/>
Finally, don't forget the network permissions
<uses-permission android:name= "Android.permission.INTERNET"/>
This article is from the "climb over the mountains to see the Sea" blog, please be sure to keep this source http://670176656.blog.51cto.com/4500575/1865183
Android and JS interaction issues