Android JS Intermodulation

Source: Internet
Author: User

 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

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.