Cross-platform asynchronous invocation instance between Android and JS _android

Source: Internet
Author: User

Cross-platform asynchronous invocation between Android and JS

Why the sudden need to do this problem?

It took nearly 1 days to figure out the solution to the problem of dog blood when developing a browser, and Android and Javascirpt were intermodulation.

Because the interface is crawled by someone else, so there is a JS cross-domain problem, Android shiny debut buttoned up.

GIF Animation demo

  

WebView Related Settings

WebSettings mwebsettings = GetSettings ();
Mwebsettings.setdefaulttextencodingname ("UTF-8");/Set the default display encoding

mwebsettings.setjavascriptenabled (TRUE); Call the JS method. Android version is greater than 17 plus annotation @JavascriptInterface



Directly enlarge recruit-> paste Code

Android

Addjavascriptinterface (New Object () {@JavascriptInterface public void toastmessage (final String URL, final i
        NT type, final int dir) {l.e ("url =" + URL + "type =" + Type + "DIR =" + dir); Apiwrapper.getinstance (). Getlenovoword (URL). Subscribeon (Schedulers.io ()). Observeon (
              Androidschedulers.mainthread ()). Subscribe (New rxsubscriber<responsebody> () {@Override public void _onnext (Responsebody responsebody) {try {String data = Respons
                  Ebody.string ();
                  L.E ("data =" + data);
                Loadurl ("javascript:rescompleted + data +", "+ Type +", "+ dir +"));
                catch (IOException e) {e.printstacktrace (); @Override public void _OnError (String msg) {Loadurl ("Javas
       cript:rescompleted ("+ msg +"));       }
            }); }, "Android");

Html

 

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.