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