Android WebView FAQ: androidwebview

Source: Internet
Author: User

Android WebView FAQ: androidwebview
1. Customize the error display interface for WebView:

/*** Display the custom error prompt page and overwrite it with a View 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. Clear cache and history:


WebView. receivache (true );
WebView. clearHistory ();

4. Determine whether the WebView has been rolled to the bottom of the page:


The getScrollY () method returns the distance from the top of the current visible area to the top of the entire page, that is, the scrolling distance of the current content.
The getHeight () or getBottom () Methods return the height of the current WebView container.
GetContentHeight returns the height of the entire html, but it is not the same as the current height of the entire page, because WebView has the zoom function, therefore, the current height of the entire page is actually the height of the original html and then multiplied by the scaling ratio. therefore, the correct result should be:
If (WebView. getContentHeight * WebView. getScale () = (webview. getHeight () + WebView. getScrollY () {// already at the bottom}

5. URL Interception:

Android WebView cannot intercept fragment redirects on the page. However, if the url is redirected, the page will be refreshed, And the H5 page experience will decrease. You can only inject JS methods to WebView.

6. Process non-hyperlink requests in WebView (such as Ajax requests):


Sometimes you need to add a request header, but it is not a hyperlink request, there is no way to intercept in shouldOverrinding and use the webView. loadUrl (String url, HashMap headers) method to add a Request Header

Currently, a temporary solution is used:

First, add special tags/protocols to the url. For example, intercept the corresponding request in the onWebViewResource method, and then splice the request header to be added to the end of the url in the form of get.

In the shouldInterceptRequest () method, resource requests on all webpages can be intercepted, such as loading JS, images, and Ajax requests.

Ex:
@ SuppressLint ("NewApi ")
@ Override
Public WebResourceResponse shouldInterceptRequest (WebView view, String url ){
// The request header cannot be directly added for non-hyperlink (such as Ajax) requests. The request header is spliced to the end of the url. Here, an imei is spliced as an example.
String ajaxUrl = url;
// ID: req = ajax
If (url. contains ("req = ajax ")){
AjaxUrl + = "& imei =" + imei;
}
Return super. shouldInterceptRequest (view, ajaxUrl );
}

7. Show pictures first on 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 is displayed when an image is requested
MEventListener. onWebViewEvent (CustomWebView. this, OnWebViewEventListener. EVENT_ON_HIDE_PROGRESS, view. getUrl ());
}
Super. onLoadResource (view, url );
}

8. Blocking long-pressed events because webview will call the system's copy control on time:


MWebView. setOnLongClickListener (new OnLongClickListener (){

@ Override
Public boolean onLongClick (View v ){
Return true;
}
});

9. Add flash Support to WebView:


String temp = "+ "/"> <Br/> <embed src =/"" + url + "/" width =/"" + "100%"
+ "/" Height =/"" + "90%" + "/" scale =/"" + "noscale"
+ "/" Type =/"" + "application/x-shockwave-flash"
+ "/"> </Embed> </body> String mimeType = "text/html ";
String encoding = "UTF-8 ";
Web. loadDataWithBaseURL ("null", temp, mimeType, encoding ,"");

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.