Android basic getting started tutorial -- Precautions for WebView after Android 4.4, 7.5.3webview

Source: Internet
Author: User

Android basic getting started tutorial -- Precautions for WebView after Android 4.4, 7.5.3webview
Android basic getting started tutorial -- Precautions for WebView after Android 4.4

Tags (separated by spaces): basic Android tutorial

This section introduces:

For this section, refer to the original article: Precautions for WebView in Android 4.4. md
Starting from Android 4.4, WebView in Android is no longer based on WebKit, but is based on Chromium.
This greatly improves the performance of WebView and provides better support for HTML5, CSS, and JavaScript!
Although chromium completely replaces the previous WebKit for Android, the API of Android WebView has not changed,
It is fully compatible with earlier versions. The advantage of this is that the WebView-based APP does not need to be modified,
You can enjoy the efficiency and power of the chromium kernel.
For WebView after 4.4, pay attention to the following issues:

1. Multithreading

If you call WebView related methods in the Child thread instead of the UI thread, unexpected errors may occur.
Therefore, when multithreading is required in your program, use the runOnUiThread () method to ensure that
WebView operations are performed 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 for developing Android programs. Although it is truth, we often do not consciously
A common mistake in development is to wait for JavaScript callback in the UI thread.
For example:

// This code is BAD and will block the UI threadwebView.loadUrl("javascript:fn()"); while(result ==null){  Thread.sleep(100); }

Do not do this. In Android 4.4, a new Api is provided to do this.
EvaluateJavascript () is specifically used to asynchronously execute JavaScript code.

3. evaluateJavascript () method

It is used to call JavaScript methods asynchronously and get a callback result.

Example:

mWebView.evaluateJavascript(script, new ValueCallback<String>() { @Override public void onReceiveValue(String value) {      //TODO }});
4. Process url redirection in WebView

In the new version of WebView, more stringent restrictions are added for url redirection of custom scheme. When you implement the shouldOverrideUrlLoading () or shouldInterceptRequest () callback, WebView will only jump when the jump url is a legal Url.
For example, if you use a url like this:

<a href="showProfile">Show Profile</a>

ShouldOverrideUrlLoading () will not be called.
The correct method is:

<a href="example-app:showProfile">Show Profile</a>

The corresponding method for detecting Url redirection:

// 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, you can also use it like this:

webView.loadDataWithBaseURL("example-app://example.co.uk/", HTML_DATA,null,"UTF-8",null);
5. UserAgent changes

If the server program corresponding to your App will do different things based on the UserAgent sent from the client, you need to pay attention
In the new version of WebView, The 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.0Mobile Safari/537.36

UseGetdefauseruseragent ()You can obtain the default UserAgent by using the following methods:

mWebView.getSettings().setUserAgentString(ua);mWebView.getSettings().getUserAgentString();

To set and obtain the custom UserAgent.

6. Considerations for using addJavascriptInterface ()

Start with Android4.2. Only Java Methods declared by @ JavascriptInterface 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 version of WebView also provides a very powerful function: Using Chrome to debug your program running in WebView
For details, see remote-debugging.
PS: Ladder required ~ You can also directly Learn About Baidu remote-debugging and how to use it!

Solve the Problem of N5 reading contacts in the previous section:

Hey, after reading the above, we know that after Android4.2, only @ JavascriptInterface is added
The declared Java method can be called by JavaScript, So we add @ JavascriptInterface to the previous two methods.

However, after adding the contact list, it does not appear as we expected. Why?
We can view the Log to find the following information:

This means that all WebView methods should be called in the same thread, while the contactlist method here is
The JavaBridge thread is called! Therefore, we need to write the stuff in contactlist to the same thread, such as a solution
The method is as follows:

Hey hey, next we run the program, and the magic found that our N5 mobile phone contacts can read ~

Similarly, the first example can also be used in this way ~

Summary:

This section describes the precautions for WebView after Android 4.4 and some N5 problems in the previous section.
Solution ~ I believe it will facilitate the use of WebView in actual development ~ Thank you.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.