First, WebView overview

Android WebView is a special View on the Android platform, which can be used to display web pages. This WebView class can be used to display only an online web page in an app, and of course can also be used to develop a browser.

WebView internal implementation is the use of rendering engine (WebKit) to display the content of the view, provide web page forward and backward, web page zoom, zoom, search and other functions.

WebView is a control based on WebKit engine to display Web pages. Android WebView uses different Kernel versions of WebKit versions in the lower version and higher version.

Two, through the pit

  1. Android API level 16 remote code execution vulnerability and previous version, we do not have the right to restrict the use of the vulnerability stems from a program WebView. AddJavascriptInterface method, a remote attacker can be through the use of the Java Reflection API, Exploit this vulnerability to execute methods on arbitrary Java objects.
  2. Use the addView method to dynamically add the WebView to the LinearLayout file. When leaving the activity, you need to destroy the WebView so as not to leak memory.
  3. Jsbridge builds a bridge through JavaScript. One end of the bridge is the Web end, and the other end is the client Native end. The purpose of the bridge is to make the local native end call the remote Web JS code and the remote Web end call the client Native code.
  4. WebviewClient. OnPageFinished — — > WebChromeClient. OnProgressChanged

WebviewClient. OnPageFinished said page load is complete callback the method, it can determine whether the content is really finished loading, the current is called url to produce jump, this method will be called many times, so when the webview to load all kinds of web pages, And operation is carried out on the web page, it is best to call WebChromeClient. OnProgressChanged method, this method on some. 5. Background power consumption: When the program starts WebView to load web pages, WebView will start threads. If the WebView is not properly destroyed, these residual threads will always run in the background, resulting in high power consumption of the application. 6. WebView hardware acceleration leads to page rendering problems. Hardware acceleration starts from android3.0. After enabling hardware acceleration, WebView renders pages faster and drags them more smoothly. The solution to this problem is to set up the WebView to turn off hardware acceleration.

3. WebView memory leak problem

3.1 Why do MEMORY leaks occur

A WebView is associated with an activity, and the actions performed by the WebView are in a new thread. There is no way for the activity to determine its time. The WebView will always hold a reference to the Activity, which cannot be reclaimed.

3.2 Two ways to solve memory leaks

  1. Independent processes, simple and violent, but can be designed to communicate between processes.
  2. Adding a WebView dynamically, using a weak reference to the Context used in the WebView passed in, adding a WebView dynamically means adding a WebView when the layout is created and the ViewGroup is used to hold the WebView, when the Activity is created, Remove the WebView when the Activity stops. Destroy the WebView and set the WebView to NULL.

Resources Android WebView usage (super detailed usage)