Since Android 4.4, the WebView in Android has started to support a series of functions of the browser based on Chromium ( Google Chrome ), and webkit parses each node of the web page. This change greatly improves the performance of WebView, and supports HTML5, CSS3, and JavaScript. With better support.
The case mainly introduces that WebView loads web pages with HTML5 video tags, clicks the links in the web pages or jumps in the current webview, does not jump to the browser, and prevents WebView memory leaks.
Effect picture:
Html web page map:
code
public class MainActivity extends Activity { private WebView webView; private String url = "http://lbh.zhangwoo.cn/?m=home&c=index&a=home"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); initWebView(); } @SuppressWarnings("deprecation") @SuppressLint("SetJavaScriptEnabled") private void initWebView() { webView = (WebView) findViewById(R.id.activity_webview); webView.requestFocus(); webView.setHorizontalScrollBarEnabled(false); webView.setVerticalScrollBarEnabled(false); WebSettings web = webView.getSettings(); web.setJavaScriptEnabled(true); web.setBuiltInZoomControls(true); web.setSupportZoom(true); web.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); web.setUseWideViewPort(true); web.setLoadWithOverviewMode(true); web.setSavePassword (true); web.setSaveFormData(true); //web.setBlockNetworkImage(true);// Put the image loading at the end to load and render webView.loadUrl(url); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // Rewrite this method to indicate that clicking on the link in the web page still jumps in the current webview, not the browser view.loadUrl(url); return true; } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { // Override this method to allow webview to handle https requests handler.proceed(); } }); } @Override // set fallback // Override the onKeyDown(int keyCoder,KeyEvent event) method of the Activity class public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { webView.goBack(); // goBack() means to return to the previous page of WebView return true; } return false; } /*** * Prevent WebView loading memory leak */ @Override protected void onDestroy() { super.onDestroy (); webView.removeAllViews(); webView.destroy(); } }
network permissions
<uses-permission android:name="android.permission.INTERNET"/>