WebView contains the underlying HTML display functionality, which needs to be optimized in many ways.
(1) Common settings
(2) Web Client
(3) Browser client
(4) Scroll bar
(5) Access to Web content
Code:
PackageMe.chunyu.Pedometer.base;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.webkit.JavascriptInterface;ImportAndroid.webkit.JsResult;ImportAndroid.webkit.WebChromeClient;ImportAndroid.webkit.WebSettings;ImportAndroid.webkit.WebView;ImportAndroid.webkit.WebViewClient;/** * Used to control webview view * Due to the use of webview too many types, such as activity,fragment,dialogfragment and so on * so webview function independently as a view * @ Author masawong * @author wangchenlong * *@SuppressWarnings({"Unused","Weakeraccess"}) Public class pedowebview extends WebView { Private Static FinalString TAG ="DEBUG-WCL:"+ PedoWebView.class.getSimpleName ();@SuppressWarnings("Spellcheckinginspection")Private Static FinalString Js_process_tag ="Htmlout";//To get the content of HTML //For getting HTML content, fixed format {@link Myjavascriptinterface} Private Static FinalString html_content ="Javascript:window."+ Js_process_tag +". processhtml (' ;PrivateReceivedtitlelistener Mreceivedtitlelistener;PrivatePagefinishedlistener Mpagefinishedlistener;PrivateReceivederrorlistener Mreceivederrorlistener;PrivateUrlloadinglistener Murlloadinglistener;PrivateJsalertlistener Mjsalertlistener; Public Pedowebview(Context context) {Super(context);if(!isineditmode ()) init (); } Public Pedowebview(context context, AttributeSet attrs) {Super(Context, Attrs,0);if(!isineditmode ()) init (); } Public Pedowebview(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle);//Resource display if(!isineditmode ()) init (); }/** * constructor, configuration WebView * / @SuppressLint("Addjavascriptinterface") Public void Init() {//ClearView () is deprecated, but onbackpressed returns to About:blank //noinspection DeprecationClearView (); Setwebviewclient (); Setwebchromeclient (); Setwebviewsettings (); Sethorizontalscrollbarenabled (false);//scroll bar does not occupy a bitSetscrollbarstyle (View.scrollbars_inside_overlay); Addjavascriptinterface (NewMyjavascriptinterface (), Js_process_tag); }/** * Configure webview parameters * / @SuppressLint("Setjavascriptenabled")protected void setwebviewsettings() {WebSettings settings = GetSettings ();//User settingsSettings.setjavascriptenabled (true);//Allow pop -up windowsSettings.setloadsimagesautomatically (true); Settings.setusewideviewport (true); Settings.setloadwithoverviewmode (false);//Technical settingsSettings.setsupportmultiplewindows (true);//Support multi-windowSettings.setappcacheenabled (true); Settings.setdatabaseenabled (true); Settings.setdomstorageenabled (true);//Priority use of cacheSettings.setcachemode (websettings.load_cache_else_network); }/** * Configure Webviewclient to handle various states of page loading */ protected void setwebviewclient() {Webviewclient webClient =NewWebviewclient () {//redirect will load multiple times @Override Public void onpagefinished(WebView view, String URL) {getsettings (). Setblocknetworkimage (false);if(Mpagefinishedlistener! =NULL{mpagefinishedlistener.overridepagefinished (view, URL); } loadurl (Html_content);//Loading JS content}@Override Public void Onreceivederror(WebView view,intErrorCode, string description, String failingurl) {if(Mreceivederrorlistener! =NULL) {Mreceivederrorlistener.overridereceivederror (view, ErrorCode, Descriptio n, Failingurl); } }@Override Public Boolean shouldoverrideurlloading(WebView view, String URL) {returnMurlloadinglistener! =NULL&& murlloadinglistener.overrideurlloading (view, URL); } }; Setwebviewclient (webClient); }/** * Get JS content * /Class Myjavascriptinterface {@JavascriptInterface @SuppressWarnings("Unused") Public void processhtml(String HTML) {LOG.D (TAG,"Consult the Doctor's H5 page:"+ HTML); } }/** * Configure Webchromeclient to handle Jsalert, which can be used to get some complex data from Web pages */ protected void setwebchromeclient() {Webchromeclient webchromeclient =NewWebchromeclient () {@Override Public void Onreceivedtitle(WebView view, String title) {if(Mreceivedtitlelistener! =NULL{mreceivedtitlelistener.onreceivedtitle (view, title); } }@Override Public Boolean Onjsalert(WebView view, string URL, string message, jsresult result) {returnMjsalertlistener! =NULL&& mjsalertlistener.overridejsalert (view, URL, message, result); } }; Setwebchromeclient (webchromeclient); }/** * Load URL * * @param URL required to load the URL */ @Override Public void Loadurl(String URL) {//load the image and load the rendering at the endGetSettings (). Setblocknetworkimage (true);Super. Loadurl (URL); } Public void Setreceivedtitlelistener(Receivedtitlelistener Receivedtitlelistener) {Mreceivedtitlelistener = Receivedtitlelistener; } Public void Setpagefinishedlistener(Pagefinishedlistener Pagefinishedlistener) {Mpagefinishedlistener = Pagefinishedlistener; } Public void Setreceivederrorlistener(Receivederrorlistener Receivederrorlistener) {Mreceivederrorlistener = Receivederrorlistener; } Public void Seturlloadinglistener(Urlloadinglistener Urlloadinglistener) {Murlloadinglistener = Urlloadinglistener; } Public void Setjsalertlistener(Jsalertlistener Jsalertlistener) {Mjsalertlistener = Jsalertlistener; } Public interface receivedtitlelistener { voidOnreceivedtitle (WebView view, String title); } Public interface pagefinishedlistener { voidOverridepagefinished (WebView view, String URL); } Public interface receivederrorlistener { voidOverridereceivederror (WebView view,intErrorCode, string description, String failingurl); } Public interface urlloadinglistener { BooleanOverrideurlloading (WebView view, String URL); } Public interface jsalertlistener { BooleanOverridejsalert (WebView view, string URL, string message, jsresult result); }}
Reference: http://www.pedant.cn/2014/09/10/webview-optimize-points/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android-Optimize WebView page