Android Basics Getting Started tutorial--7.5.3 Android 4.4 after webview some caveats
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
This section refers to the original: WebView use precautions in Android 4.4. MD
Starting with Android 4.4, Android WebView is no longer based on WebKit, but starts with chromium, a change
The performance of WebView greatly improved, and the HTML5,CSS,JAVASCRIPT has better support!
Although chromium completely replaced the previous WebKit for Android, the Android WebView API interface did not change,
Fully compatible with the old version. The benefits are built-in apps based on WebView, without any changes,
You can enjoy the efficiency and power of the chromium kernel.
For the WebView after 4.4, we need to pay attention to the following questions:
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 multithreading in your program, use the Runonuithread () method to ensure that you
The operation of WebView is done in the UI thread:
runOnUiThread(newRunnable(){@Overridepublicvoid run(){ //CodeforWebView 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 to wait for JavaScript callbacks in the UI thread.
For example:
andthe UI threadwebView.loadUrl("javascript:fn()"while(result ==null){ Thread.sleep(100); }
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 :
newpublicvoidonReceiveValuevalue) { //TODO }});
4. Handling a jump from a URL 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:
<a href="showProfile">Show Profile</a>
Shouldoverrideurlloading () will not be called.
The correct way to use it is:
<a href="example-app:showProfile">Show Profile</a>
The corresponding way to detect the URL jump:
// The URL scheme should be non-hierarchical (no trailing slashes) privatestaticfinalString APP_SCHEME ="example-app:"@Override publicboolean shouldOverrideUrlLoading(WebView view,String url){ if(url.startsWith(APP_SCHEME)){ urlData =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_DATA,null,"UTF-8",null);
5.UserAgent Change
If your app corresponds to a service-side program that will do different things based on the useragent from the client, then you need to be aware
Is that in the new version of WebView, UserAgent has some subtle changes:
Mozilla/5.04.44 Build/KRT16H)AppleWebKit/537.36Version/4.0 Chrome/30.0.0.0Mobile 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 publictoStringreturn"injectedObject"; }}webView.addJavascriptInterface(new"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
Specific can be seen: remote-debugging
PS: Need Ladder ~ You can also direct Baidu remote-debugging to learn about the relevant information, and how to use!
In the previous section, N5 read the contact's problem resolution:
Hey, after reading the above, we know that after Android4.2, only add @JavascriptInterface
The declared Java method can be called by JavaScript, so we add @javascriptinterface to the previous two methods
But, after the addition, and not as we expected, the list of contact people we want to appear, this is why?
We found the following piece of information by looking at log:
Probably means that all webview methods should be called in the same line Cheng, and here the ContactList method is
Javabridge is called in the thread! So we're going to write the contactlist in the same thread, like a solution
method, which is the following:
Hey, next Run the program, the magical discovery, our N5 phone contact can read the ~
Similarly, the first example can be resolved as well
This section summarizes:
This section has a trip with you after Android 4.4 WebView to note, as well as some of the N5 issues in the previous section
Resolution ~ Believe will give you in the actual development of the use of WebView bring convenience ~ Thank you
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--7.5.3 Android 4.4 after webview some caveats