Android-WebView optimization page

Source: Internet
Author: User

Android-WebView optimization page

WebView includes the basic HTML display function, which needs to be optimized in many aspects during use.
(1) Common settings
(2) webpage Client
(3) browser client
(4) scroll bar
(5) Obtain webpage content

Code:

Package me. chunyu. pedometer. base; import android. annotation. suppressLint; import android. content. context; import android. util. attributeSet; import android. util. log; import android. view. view; import android. webkit. javascriptInterface; import android. webkit. jsResult; import android. webkit. webChromeClient; import android. webkit. webSettings; import android. webkit. webView; import android. webkit. webViewClient;/*** used to control the View of WebView * because there are too many types of WebView, such as Activity, Fragment, dialogFragment and so on * separate the WebView function as a View ** @ author MasaWong * @ author wangchenlong */@ SuppressWarnings ({unused, WeakerAccess }) public class PedoWebView extends WebView {private static final String TAG = DEBUG-WCL: + PedoWebView. class. getSimpleName (); @ SuppressWarnings (SpellCheckingInspection) private static final String JS_PROCESS_TAG = HTMLOUT; // get HTML content, fixed format {@ link MyJavaScriptInterface} private static final String HTML_CONTENT = javascript: window. + JS_PROCESS_TAG +. processHTML ('
'+ Document. getElementsByTagName ('html') [0]. innerHTML +' '); Private listener ederrorlistener mlistener ederrorlistener; private UrlLoadingListener mUrlLoadingListener; private JsAlertListener listener; 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, int defStyle) {super (context, attrs, defStyle); // if (! IsInEditMode () init ();}/*** constructor, configure WebView */@ SuppressLint (AddJavascriptInterface) public void init () {// clearView () is deprecated, but onBackPressed returns to about: blank // noinspection deprecation clearView (); setWebViewClient (); setWebChromeClient (); setWebViewSettings (); setHorizontalScrollBarEnabled (false ); // The scroll bar is not placeholder setScrollBarStyle (View. SCROLLBARS_INSIDE_OVERLAY); addJavascriptInter Face (new MyJavaScriptInterface (), JS_PROCESS_TAG);}/*** configure the WebView parameter */@ SuppressLint (SetJavaScriptEnabled) protected void setWebViewSettings () {WebSettings settings = getSettings (); // User settings. setJavaScriptEnabled (true); // allows pop-up settings. setLoadsImagesAutomatically (true); settings. setUseWideViewPort (true); settings. setLoadWithOverviewMode (false); // Technical settings. s EtSupportMultipleWindows (true); // supports multi-window settings. setAppCacheEnabled (true); settings. setDatabaseEnabled (true); settings. setDomStorageEnabled (true); // cache settings is preferred. setCacheMode (WebSettings. LOAD_CACHE_ELSE_NETWORK);}/*** configure WebViewClient to handle various states of web page loading */protected void setWebViewClient () {WebViewClient webClient = new WebViewClient () {// redirection loads @ Override public void onPageFinished (WebView vi Ew, String url) {getSettings (). setBlockNetworkImage (false); if (mPageFinishedListener! = Null) {mPageFinishedListener. overridePageFinished (view, url);} loadUrl (HTML_CONTENT); // load JS content} @ Override public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {if (mReceivedErrorListener! = Null) {mReceivedErrorListener. overrideReceivedError (view, errorCode, description, failingUrl) ;}@ Override public boolean shouldOverrideUrlLoading (WebView, String url) {return mUrlLoadingListener! = 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 process JsAlert, used to obtain some complex data from a webpage */protected void setWebChromeClient () {WebChromeClient webChromeClient = new WebChrom EClient () {@ Override public void onReceivedTitle (WebView view, String title) {if (mReceivedTitleListener! = Null) {mshortedtitlelistener. onshortedtitle (view, title) ;}@override public boolean onJsAlert (WebView view, String url, String message, JsResult result) {return mJsAlertListener! = Null & mJsAlertListener. overrideJsAlert (view, url, message, result) ;}}; setWebChromeClient (webChromeClient );} /*** load Url ** @ param url the url to be loaded */@ Override public void loadUrl (String url) {// load the image to the end to load and render getSettings (). setBlockNetworkImage (true); super. loadUrl (url);} public void listener (listener) {listener = ReceivedTitleListener;} public void listener (listener) {listener = listener;} public void setlistener ederrorlistener (listener ederrorlistener) {listener = receivedErrorListener;} public void listener (UrlLoadingListener urlLoadingListener) {mUrlLoadingListener = urlLoadingListener;} public void listener (JsAlertListener jsAlertListener) {listener = listener ;} public interface extends edtitlelistener {void onReceivedTitle (WebView view, String title);} public interface PageFinishedListener {void listener (WebView view, String url);} public interface extends ederrorlistener {void listener (WebView view, int errorCode, String description, String failingUrl);} public interface UrlLoadingListener {boolean overrideUrlLoading (WebView view, String url);} public interface JsAlertListener {boolean overrideJsAlert (WebView, String url, string message, JsResult result );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.