A Brief analysis of Android webview

  • 2021-01-03 21:04:18
  • OfStack

This paper briefly analyzes the usage of Android webview. To share for your reference, the details are as follows:

Android has a high-performance webkit kernel browser built into the Android phone, which encapsulates components known as WebView in SDK.

WebView use:

(1) Add permission: The license "android.permission.INTERNET" must be used in ES14en.xml, otherwise the error of Web page not available will occur.
(2) Generate 1 WebView component in Activity:

WebView webView = new WebView(this);

(3) Basic information of WebView setting:


webview.getSettings().setJavaScriptEnabled(true);//  Set up support Javascript
requestFocus();//  Touch focus works 
setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);//  Unscroll bar 

(4) Set the page to be displayed by WevView:


webView.loadUrl("http://www.google.com");//  The Internet 
webView.loadUrl("file:///android_asset/XX.html");//  Local files, local files are stored in: assets In the file 

(5) If you want to click the link and not open the response in browser's system Android, you need to add an event listener to WebView and rewrite the shouldOverrideUrlLoading method.


public boolean shouldOverrideUrlLoading(WebView view,String url) {
 view.loadUrl(url);
 return true;
}

Other parts of the rewritable methods:

(1) The event that received the Http request

onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm)

(2) Events before opening the link


public boolean shouldOverrideUrlLoading(WebView view, String url) {
 view.loadUrl(url);
 return true;
}

(3) Events completed by loading the page


public void onPageFinished(WebView view, String url) {
}

(4) Events at the beginning of page loading


public void onPageStarted(WebView view, String url, Bitmap favicon) {
}

This event starts the page load call, and we can usually set up an loading page here to tell the user that the application is waiting for a network response.

1. If you click the link of WebView and read many pages without any processing, click the key of System Return (Back), the whole browser will call the method of finish() and end itself. If you want to browse the web page to retreat rather than exit the browser, you need to process and consume the Back event in the current Activity.

Override the onKeyDown(int keyCoder,KeyEvent event) method of the Activity class.


public boolean onKeyDown(int keyCode,KeyEvent event){
 if(webView.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK){
  webview.goBack();// goBack() Said to return to webView On the 1 page 
  return true;
 }
 return false;
}

2. Differences in the use of loadData() and loadDataWithBaseURL()

html data in loadData() cannot contain '#', '%', '\', '? Special characters in '4. If this character occurs, a parse error will occur, showing that the page cannot be found and part of the html code.

Processing method: We need to use UrlEncoder code %23, %25, %27, %3f. The following two codes can be used, data is string type html code (1) ES93en. loadData(URLEncoder. encode(data, "utf-8 "), "text/html"," ES101en-8 "); (2) webView.loadDataWithBaseURL(null, data, "text/html", "ES108en-8 ", null);

WebView related properties:

(1) Set WebView to be transparent:


android:background="#00000000"
android:cacheColorHint="#00000000"
WebView.setBackgroundColor(0);

(2) WebView Display sd card picture:

webView.loadDataWithBaseURL(null, "", "text/html" , "utf-8", null);

(3) WebView displays the string
webView.loadDataWithBaseURL("", "", "text/html", "utf-8", "");

(4) Set the size of the font displayed in WebView


public static final TextSize[] FONT_SIZES = new TextSize[] {
 TextSize.SMALLER,
 TextSize.NORMAL,
 TextSize.LARGER
};
private WebSettings wb;
wb = mWebViewRightContent.getSettings();
wb.setTextSize(FONT_SIZES[iFontSizeId]);

Font size:


public enum TextSize {
 SMALLEST(50),
 SMALLER(75),
 NORMAL(100),
 LARGER(150),
 LARGEST(200);
 TextSize(int size) {
 value = size;
 }
 int value;
}

(5) When WebView displays html file, it is only necessary to set WebView 1 to achieve the same effect as PC displayed in the browser:

To adapt to the full screen
Adapt portrait screen
57 ADAPTS to landscape

mWebView.setInitialScale(39);

One caveat: html will show up on Android if the font is too small. Font size: 6, 7 in general.
(6) WebView setting gradient:


webView.loadUrl("http://www.google.com");//  The Internet 
webView.loadUrl("file:///android_asset/XX.html");//  Local files, local files are stored in: assets In the file 
0

(7) Set WebView to zoom in and out by touch:

mWebView.getSettings().setBuiltInZoomControls(true);

(8) WebView becomes larger by double-clicking and then smaller by double-clicking. When manually zooming in, double-clicking can restore to the original size, as shown below:
webView.getSettings().setUseWideViewPort(true);

(9) Several methods to speed up the loading of WebView and improve the priority of rendering
webView.getSettings().setRenderPriority(RenderPriority.HIGH);

use
webView.getSettings().setBlockNetworkImage
Put the image loading last to load the rendering
webView.getSettings().setBlockNetworkImage(true);

(10) To convert the string into HTML file display:


webView.loadUrl("http://www.google.com");//  The Internet 
webView.loadUrl("file:///android_asset/XX.html");//  Local files, local files are stored in: assets In the file 
1

I hope this article has been helpful in Android programming.


Related articles: