WebView usage precautions in Android 4.4

Source: Internet
Author: User

WebView usage precautions in Android 4.4

Since Android 4.4, Android WebView 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 () {@Overridepublicvoid 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 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:

Mwebview.evaluatejavascript (script, New valuecallback<string> () {@Override public void Onreceivevalue (String Value) {      //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"]]>show profile</a>

Shouldoverrideurlloading () will not be called.

The correct way to use it is:

<ahref= "Example-app:showprofile"]]>show profile</a>

The corresponding way to detect the URL jump:

The URL scheme should be non-hierarchical (no trailing slashes) private static final String App_scheme = "Example-app:"; @Override public boolean shouldoverrideurlloading (WebView view,string URL) {     if (Url.startswith (App_scheme)) {         urldata = Urldecoder.decode (url.substring (App_scheme.length ()), "UTF-8");         Respondtodata (urldata);         returntrue;     }     return false; }

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 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 (Linux; Android 4.4; Nexus 4 build/krt16h) applewebkit/537.36 (khtml, like Gecko) version/4.0 chrome/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.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

WebView usage precautions in Android 4.4

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.