Some considerations for WebView after Android 4.4

Introduction to this section:

This section refers to the original text: Precautions for using WebView in Android 4.4.md

Starting from Android 4.4, WebView in Android is no longer based on WebKit, but based on Chromium. This change has greatly improved the performance of WebView, and has better support for HTML5, CSS, and JavaScript!

Although chromium has completely replaced the previous WebKit for Android, the API interface of Android WebView has not changed and is fully compatible with the old version. The benefit brought by this is that the APP built on the basis of WebView can enjoy the efficiency and power of the chromium kernel without any modification.

For WebView after 4.4, we need to pay attention to the following issues:


1. Multithreading

If you call the related methods of WebView in the child thread instead of the UI thread, unexpected errors may occur. Therefore, when you need to use multithreading in your program, please also use the runOnUiThread() method to ensure that your operations on WebView are performed in the UI thread:

runOnUiThread(newRunnable(){
@Override
publicvoid run(){
   // Code for WebView goes here
   }
});

2. Thread blocking

Never block the UI thread, this is a truth in developing Android programs. Although it is the truth, we often make some mistakes unconsciously to violate it. A common mistake in development is: waiting for JavaScript callbacks in the UI thread. For example:

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

Don't do this, Android 4.4 provides a new API to do this. evaluateJavascript() is designed to execute JavaScript code asynchronously.


3.evaluateJavascript() method

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

Example :

mWebView.evaluateJavascript(script, new ValueCallback<String>() {
 @Override
 public void onReceiveValue(String value) {
      //TODO
 }
});

4. Handle the jump of url in WebView

The new version of WebView has added stricter restrictions on the url redirection of custom schemes. When you implement 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 way to use it is:

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

Corresponding way to detect 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;
     }
     return false;
}

Of course, it can also be used 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 according to the UserAgent sent from the client, then you need to pay attention to the fact that in the new version of WebView, the UserAgent has undergone 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

The default UserAgent can be obtained by using the getDefaultUserAgent() method, or by:

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

To set and get a custom UserAgent.


6. Precautions for using addJavascriptInterface()

Starting from Android4.2. Only Java methods declared with @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: use Chrome to debug your program running in WebView. For details, you can see: remote-debugging  PS: You need a ladder~ You can also go to Baidu remote-debugging directly to learn about related information and how to use it ! 


The solution to the problem of N5 reading contacts in the previous section:

Hey, after reading the above, we know that after Android4.2, only Java methods declared by adding @JavascriptInterface can be called by JavaScript, so we added @JavascriptInterface to the previous two methods

However, after the addition, the contact list we wanted did not appear as we expected. Why? We found the following piece of information by viewing the Log:

The general meaning is: all WebView methods should be called in the same thread, but the contactlist method here is called in the JavaBridge thread! So we have to write the stuff in the contactlist to the same thread, such as a solution, which is the following:

Hey, run the program next, and find miraculously, our N5 phone contacts can be read~

 

In the same way, the first example before can also be solved in this way~

Guess you like

Origin blog.csdn.net/leyang0910/article/details/131499831