At present, HTML5 development is very rapid, many native app will be embedded in the Web page, in order to apply the changeable market demand. But Android's webview default support is very weak, many places need customization, to achieve the desired effect. And WebView will have varying degrees of bugs in different versions. The following small series of WebView often appear in the problem for everyone to organize as follows:
1. Custom error display interface for WebView:
/** * Displays a custom error prompt page with a view overlay on WebView * * protected void Showerrorpage () {LinearLayout Webparentview = (linearlay
Out) 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 Cleaning:
Cookiesyncmanager.createinstance (this);
Cookiesyncmanager.getinstance (). Startsync ();
Cookiemanager.getinstance (). Removesessioncookie ();
3. Clean cache and historical records:
Copy Code code as follows:
Webview.clearcache (TRUE);
Webview.clearhistory ();
4. Determine if WebView has scrolled to the bottom of the page:
The getscrolly () method returns the distance at the top of the entire page from the current visible range, which is the current content scrolling distance.
The GetHeight () or Getbottom () method returns the height of the current WebView container
Getcontentheight returns the height of the entire HTML, but not the height of the current entire page, because the WebView has zoom capability, so the height of the current entire page should actually be the height of the original HTML and then multiply the scale. Therefore, the correct method of judging the result should be:
if (webview.getcontentheight*webview.getscale () = = (Webview.getheight () +webview.getscrolly ())) {//already at bottom}
5.URL Intercept:
The Android WebView is blocked from fragment jumps within the page. But the URL jumps, will cause the page to refresh, the H5 page experience has dropped again. Can only inject the JS method to WebView.
6. Process non-hyperlink requests in WebView (such as AJAX requests):
Sometimes you need to add a request header, but a request that is not a hyperlink, there is no way to intercept in shouldoverrinding and use the Webview.loadurl (String url,hashmap Headers) method for adding a request header
At present, a temporary solution is used:
First you need to add special tags/protocols in the URL, such as intercepting the corresponding request in the Onwebviewresource method, and then the request header to be added to the end of the URL in get form
In the Shouldinterceptrequest () method, you can intercept resource requests in all Web pages, such as loading JS, images, Ajax requests, and so on.
Ex:
@SuppressLint ("Newapi")
@Override public
webresourceresponse shouldinterceptrequest (WebView view, String url) {
//non-hyperlink (such as Ajax) request cannot directly add the request header, now stitching to the end of the URL, where the concatenation of a IMEI as an example
String ajaxurl = URL;
such as ID: Req=ajax
if (url.contains ("Req=ajax")) {
Ajaxurl + = "&imei=" + Imei;
}
Return super. Shouldinterceptrequest (view, 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, onwebvieweventlistener.event_on_hide_progress, View.geturl ());
Super. Onloadresource (view, URL);
8. Shielding off the long press event because WebView will call the system's replication controls on time:
Mwebview.setonlongclicklistener (New Onlongclicklistener () {
@Override public
boolean Onlongclick (View v) { return
true;
}
});
9. Add flash support in WebView:
The above content is this article to the Android WebView common problem and the processing plan entire narration, hoped everybody likes.