Cross-platform asynchronous calls between Android and JS, and androidjs asynchronous calls

Source: Internet
Author: User

Cross-platform asynchronous calls between Android and JS, and androidjs asynchronous calls
Why is this problem suddenly solved?

It took nearly one day to come up with this solution when developing a browser. Android and cirpt interact with each other.

Because the interface is captured by others, a JS cross-domain problem occurs, and Android debuted.

 

GIF animation demonstration

  

 

WebView settings
WebSettings mWebSettings = getSettings();
MWebSettings. setdefatextextencodingname ("UTF-8"); // sets the default display Encoding

MWebSettings. setJavaScriptEnabled (true); // call the JS method. The Android version is later than 17, and the annotation @ JavascriptInterface is added.


Zoom in directly-> paste code

 Android

  

1 addJavascriptInterface (new Object () {2 @ JavascriptInterface 3 public void toastMessage (final String url, final int type, final int dir) {4 L. e ("url =" + url + "type =" + type + "dir =" + dir); 5 APIWrapper. getInstance () 6. getLenovoWord (url) 7. subscribeOn (Schedulers. io () 8. observeOn (AndroidSchedulers. mainThread () 9. subscribe (new RxSubscriber <ResponseBody> () {10 @ Override11 public void _ onNext (ResponseBody responseBody) {12 try {13 String data = responseBody. string (); 14 L. e ("data =" + data); 15 loadUrl ("javascript: ResCompleted (" + data + "," + type + "," + dir + ")"); 16} catch (IOException e) {17 e. printStackTrace (); 18} 19} 20 21 @ Override22 public void _ onError (String msg) {23 loadUrl ("javascript: ResCompleted (" + msg + ")"); 24} 25}); 26} 27}, "Android ");View Code

 

 Html

 

1  

 

 

 

 

 

 

 

 

 

 

 

 
 

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.