Since Android 4.4, WebView in Android has started based on chromium (presumably because the head of the Android department has changed from Andy Rubin to the head of the Chrome department Sundar Pichai, ^_^).
This change has led to a dramatic increase in webview performance and better support for HTML5, CSS3, and JavaScript.
So, as a client developer, what do we need to be aware of when we write code?
1. Multithreading
If you call WebView's related methods in a child thread instead of the UI thread, you may have unexpected errors.
So, when you need to use multithreading in your program, use the Runonuithread () method to ensure that your operations on WebView are done in the UI thread:
Runonuithread (Newrunnable () { @Override publicvoid Run () { //Code for WebView goes here }});
2. Thread Blocking
Never block the UI thread, which is a truth to developing Android programs. Although it is the truth, we often do not consciously make some mistakes against it, a common mistake in development is: In the UI thread to wait for the JavaScript callback.
For example:
//This code was bad and would block the UI thread webView. Loadurl ("javascript:fn ()"while= =null) { Thread . Sleep (+); }
Never do this, Android 4.4, provides a new API to do this thing. Evaluatejavascript () is specifically designed to execute JavaScript code asynchronously.
3.evaluateJavascript () method
Specifically used to call JavaScript methods asynchronously, and to get a callback result.
Example:
Mwebview. New valuecallback<String> () { @Overridepublic voidonreceivevalue(Stringvalue ) { //todo }});
4. Handling URL jumps in WebView
The new version of WebView adds stricter restrictions for URL jumps to custom scheme. When you implement the shouldoverrideurlloading () or Shouldinterceptrequest () callback, WebView will only jump when the jump URL is a legitimate URL.
For example, if you use a URL like this:
<ahref="showprofile"]>Showprofile </a>
Shouldoverrideurlloading () will not be called.
The correct way to use it is:
<ahref="example-app:showprofile"]> Showprofile</a>
The corresponding way to detect the URL jump:
//The URL scheme should be non-hierarchical (no trailing slashes) App_scheme = "example-app:"; @Override publicboolean shouldoverrideurlloading (WebView view,String url) { if (URL. ) StartsWith (app_scheme)) { =urldecoder. Decode (URL. SUBSTRING (app_scheme. Length ()),"UTF-8"); Respondtodata (urldata); returntrue; } Returnfalse; }
Of course, this can also be used:
WebView. Loaddatawithbaseurl ("example-app://example.co.uk/"html_datanull, "UTF-8",null);
5.UserAgent Change
If your app corresponds to a service-side program that will do different things depending on the useragent from the client, then you need to be aware that in the new version of WebView, UserAgent has some subtle changes:
Mozilla / 5.0 (LinuxAndroid4.4Nexus4Build/ krt16h)applewebkit/537.36(khtmlGeckoVersion /4.0Chrome/30.0. 0.0 Mobile Safari / 537.36
You can use the Getdefaultuseragent () method to get the default useragent or by:
Mwebview. GetSettings (). Setuseragentstring (UA); Mwebview. GetSettings (). Getuseragentstring ();
To set and get the custom useragent.
6. Precautions for using Addjavascriptinterface ()
Starting from Android4.2. Only Java methods that add @JavascriptInterface declarations can be called by JavaScript, for example:
class Jsobject{@JavascriptInterface Public String toString() {return "injectedobject" ; }} WebView.Addjavascriptinterface (New Jsobject(), "injectedobject" ); WebView.LoadData ("", "text/html" ,NULL); WebView.Loadurl ( "Javascript:alert (injectedobject.tostring ())" );
7.Remote debugging
The new WebView also offers a powerful feature: use Chrome to debug your programs running in WebView.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Some considerations for Android 4.4 after WebView