Android WebView H5 Development Supplements

The last article introduced some WebView settings, this article is some supplementary items.

 

When the HTML5 page is loaded first, the page will be blank because the DOM storage API function needs to be enabled:

webSettings.setDomStorageEnabled(true);

 

Secondly, for the security vulnerabilities that need to be paid attention to in development, see "How to Design an Elegant and Robust Android WebView" for details :

	@TargetApi(11)
	private static final void removeJavascriptInterfaces(WebView webView) {
		try {
			if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 17) {
				webView.removeJavascriptInterface("searchBoxJavaBridge_");
				webView.removeJavascriptInterface("accessibility");
				webView.removeJavascriptInterface("accessibilityTraversal");
			}
		} catch (Throwable tr) {
			tr.printStackTrace();
		}
	}

 

Third, other points to note:

  1. Do you need to enable JS, and whether to allow file access and save passwords:
    		// Enable Javascript
    		WebSettings webSettings = mWebView.getSettings();
    		webSettings.setJavaScriptEnabled(true);
    		webSettings.setAllowFileAccess(false);
    		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    			webSettings.setAllowFileAccessFromFileURLs(false);
    		}
    
    webSettings.setSavePassword(false);
    

      

  2. Cache related:
    		if (NetWorkDetector.isConnected(this.getActivity())) {
    			// Determine whether to fetch data from the network according to cache-control.
    			webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    		} else {
    			// If there is no network, get it locally, that is, offline loading
    			webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    		}
    		// Enable DOM storage API function
    		webSettings.setDomStorageEnabled(true);
    		// Enable database storage API function
    		webSettings.setDatabaseEnabled(true);
    		// Enable Application Caches function
    		webSettings.setAppCacheEnabled(true);
    
    		// Set the Application Caches cache directory
    		webSettings.setAppCachePath(this.getActivity().getDir("appcache", MODE_PRIVATE ).getPath());
    

     

  3.  Above 5.1, the mixed use of https and http is prohibited by default. The following method is to enable

    		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    			webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    		}
    

      

  4. When the Activity is destroyed ( WebView ), first let the WebView load the null content, then remove the WebView, then destroy the WebView, and finally empty it:
    @Override
        protected void onDestroy() {
            if (mWebView != null) {
                mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
                mWebView.clearHistory();
    
                ((ViewGroup) mWebView.getParent()).removeView(mWebView);
                mWebView.destroy();
                mWebView = null;
            }
            super.onDestroy ();
        }
    
    Author: Carson_Ho
    Link: https://www.jianshu.com/p/3c94ae673e2a
    Source: Jianshu
    Copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325236803&siteId=291194637