Nine common problems with Android WebView

Source: Internet
Author: User

There are more and more problems with Android WebView, and the next step is to introduce some common Android WebView issues by Www.ineice.com's technical engineers:

1. Custom error display interface for WebView:/** * Displays the custom error prompt page, with a view overlay in WebView */protected void Showerrorpage () {LinearLayout Webparentview = (LinearLayout) mwebview.getparent ();

Initerrorpage (); while (Webparentview.getchildcount () >) {Webparentview.removeviewat ();} Linearlayout.layoutparams LP = new Linearlayout.layoutparams (layoutparams.fill_parent,layoutparams.fill_parent); Webparentview.addview (Merrorview,, LP); miserrorpage = true;} protected void Hideerrorpage () {LinearLayout Webparentview = (linearlayout) mwebview.getparent (); miserrorpage = false; while (Webparentview.getchildcount () >) {Webparentview.removeviewat ();}} protected void Initerrorpage () {if (Merrorview = = null) {Merrorview = View.inflate (this, r.layout.online_error, null); Button button = (button) Merrorview.findviewbyid (r.id.online_error_btn_retry); Button.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {mwebview.reload ();}}); Merrorview.setonclicklistener (null);}}

2.WebView Cookies Cleanup: cookiesyncmanager.createinstance (this); Cookiesyncmanager.getinstance (). Startsync (); Cookiemanager.getinstance (). Removesessioncookie ();

3. Cleanup cache and History: Webview.clearcache (True); Webview.clearhistory ();

4. Determine if WebView has scrolled to the bottom of the page: The Getscrolly () method returns the distance from the top of the entire page of the current visible area, that is, the distance the current content scrolls. The GetHeight () or Getbottom () method returns the height of the current WebView container, Getcontentheight returns the height of the entire HTML, but is not the same as the height of the current entire page, because WebView has the zoom function, So the height of the current entire page should actually be the height of the original HTML and multiply the scale. Therefore, the corrected result, the exact method of judging should be: if (webview.getcontentheight*webview.getscale () = = (Webview.getheight () +webview.getscrolly () ) {//already at bottom}

5.URL interception: Android WebView is not blocking the fragment jump within the page. But the URL jumps, also causes the page to refresh, the H5 page experience drops again. Can only inject JS method to WebView.

6. Handling non-hyperlink requests in WebView (such as AJAX requests): Sometimes you need to add a request header, but a non-hyperlink request, there is no way to intercept shouldoverrinding and Webview.loadurl (String Url,hashmap Headers) method to add a request header

At present, a temporary solution is used:

You first need to add a special tag/protocol to the URL, such as intercepting the corresponding request in the Onwebviewresource method, and then adding the request header to the end of the URL in a Get form

In the Shouldinterceptrequest () method, you can intercept all the resource requests in the Web page, such as loading JS, images, Ajax requests, etc.

Ex: @SuppressLint ("Newapi") @Override public webresourceresponse shouldinterceptrequest (WebView view,string URL) {//non- Hyperlinks (such as AJAX) requests cannot be added directly to the request header, now stitching to the end of the URL, here stitching an IMEI as an example String ajaxurl = URL; As identified: Req=ajax if (Url.contains ("Req=ajax")) {Ajaxurl + = "&imei=" + IMEI;} return super. Shouldinterceptrequest (V Iew, Ajaxurl); }

7. Display the picture first in the page: @Override public void Onloadresource (WebView view, String URL) {meventlistener.onwebviewevent ( Customwebview. This, onwebvieweventlistener.event_on_load_resource, URL); if (Url.indexof (". jpg") >) {hideprogress ();//The page meventlistener.onwebviewevent is displayed when the picture is requested (Customwebview. This, ONWEBV Ieweventlistener.event_on_hide_progress, View.geturl ()); } super. Onloadresource (view, URL); }

8. Block off long press events because WebView will call the system's copy control on time: Mwebview.setonlongclicklistener (new Onlongclicklistener () {

@Overridepublic boolean Onlongclick (View v) {return true;}});

9. Add flash support in WebView: String temp = "<body bgcolor=/" "+" BLACK "+"/"><embed src=/" "+ URL +"/"width=/" "+" 10 0% "+"/"height=/" "+" 90% "+"/"scale=/" "+" Noscale "+"/"type=/" "+" Application/x-shockwave-flash "+"/">"; String MimeType = "text/html"; String encoding = "Utf-8″; Web.loaddatawithbaseurl ("null", temp, MimeType, Encoding, "");

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nine common problems with Android WebView

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.