Public classBoatsactivityextendsActivity {Handler Mhandler=NewHandler ();//Handler @SuppressLint for processing messages ("Setjavascriptenabled") protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); This. Settitle ("Shipping Enquiry page"); FinalWebView Mwebview =NewWebView ( This);//Create a WebView object and use it as an Activity display page you can also put a WebView control in the XML layout file to initialize Setcontentview (Mwebview) here; Mwebview.loadurl ("Http://192.168.0.190:8080/loginDemo_gd/bots.html");//load the page you want to display Mwebview.getsettings (). setjavascriptenabled (true);//Gets the setting object in the WebView object set to support JavaScript mwebview.addjavascriptinterface (NewObject () {@JavascriptInterface//SDK version greater than 17 to add this comment access security considerations Public voidclickonandroid () {Mhandler.post (NewRunnable () { Public voidrun () {Toast.maketext (boatsactivity. This, "Please enter the ship you want to inquire about", Toast.length_short). Show (); } }); } @JavascriptInterface Public voidshowmsg () {Mhandler.post (NewRunnable () { Public voidrun () {Toast.maketext (boatsactivity. This, "Query succeeded", Toast.length_short). Show (); } }); } @JavascriptInterface Public voidShowresult () {Mhandler.post (NewRunnable () { Public voidrun () {Toast.maketext (boatsactivity. This, "no query to the corresponding data", Toast.length_short). Show (); } }); } @JavascriptInterface Public voidalert () {Mhandler.post (NewRunnable () { Public voidrun () {Toast.maketext (boatsactivity. This, "Please enter the ship's ID", Toast.length_short). Show (); } }); } }, "Demo");//class alias JS to call Window.demo. Method Name (JS calls Java method)
Java Call JS
Webview.loadurl ("Javascript:window.handler.show" (document.getElementById (' id '). Value,document.getelementbyid (' Password ') (value) ");
mwebview.setwebviewclient (new webviewclient () { @Override public void onpagefinished (WebView view, String URL) { //Toast.maketext ( Boatsactivity.this, "Loading complete", Toast.length_short). Show (); Super . onpagefinished (view, URL);}} ); }}
Android JS Intermodulation