WebView Loading Method and WebView Loading Method

Source: Internet
Author: User

WebView Loading Method and WebView Loading Method
WebView is loaded in three ways: 1> WebView. loadUrl (String strhtml); used to load the content of a webpage. Note that you need to add the permission "" to store local connections under the assets file, WebView. loadUrl ("file: // android_aasset/html/") 2> WebView. loadData (String data, String mimeType, String encoding); data: type of webpage content to be loaded by mimeType (type/html, image/jpeg) the encoding format (gbk/UTF-8) set by encoding to load network content; 3> LoadDataWithBaseURL () is generally used in WebView. loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl) is preferred for loadData

Generally, JavaScript is supported. You must set WebSessings settings = webview. getAebSettings (). You must set its attributes.

Functions of WebSettings, WebViewClient, and WebChromClient compared with WebView
1. WebSetting is used to set the implementation and status of WebView,
WebSettings webSettings = mWebView. getSettings ();
// Set to support js *******************. The default value is false.
Settings. setJavaScriptEnabled (true );
// Supports scaling.
Settings. setBuiltInZoomControls (true );
// Supports image loading.
WSet. setBlockNetworkImage (false );
// Set to support caching
Settings. setAppCacheEnabled (true );
// Set the cache mode to default
Settings. setCacheMode (WebSettings. LOAD_DEFAULT );
Settings. setCacheMode (WebSettings. LOAD_DEFAULT );

SetAllowFileAccess enable or disable WebView from accessing File data

SetBlockNetworkImage: whether to display network images

SetBuiltInZoomControls: whether scaling is supported

SetCacheMode: Set the buffer mode.

SetDefaultFontSize: Set the default font size.

SetDefaultTextEncodingName sets the default encoding used for decoding.

SetFixedFontFamily: set fixed Fonts

SetJavaScriptEnabled: Set whether Javascript is supported

SetLayoutAlgorithm

SetLightTouchEnabled sets the option to activate with the mouse

Whether the setsuppzoom zoom setting supports zoom

WebViewClient is a class dedicated to assisting WebView in handling various notifications, requests, and other events. You can use the setWebViewClient method of WebView to specify a WebViewClient object. WebViewClient provides the following methods. We can overwrite these methods to help WebView browse Web pages. The Code is as follows (we set the override shouldOverrideUrlLoading method so that when there is a new connection, use the current WebView for display ):

Public boolean shouldOverrideUrlLoading (WebView view, String url ){

View. loadUrl (url );

Return true;

} The default value is true ------ block, indicating that the current connection cannot be loaded in the current wenView. false indicates blocking.

OnPageFinished web page loaded

OnPageStarted web page loading

OnReceivedError reports error messages

OnScaleChanged WebView changed

ShouldOverrideUrlLoading controls the opening of new connections in the current WebView

Supplement: processing of the "Back" button: if you have read many pages through the webview link, if you have not done any processing, click the "Back" button of the system, the whole webView process calls finish () to end itself. If you want to roll Back the web page you want to browse rather than exit the browser, you need to process and consume the Back event in the current Activity.

Override the onKeyDown (int keyCoder, KeyEvent event) method of the Activity class.

Webview. goBack (); // goBack () indicates that the previous page of webView is returned.

After practice, I found that loadData cannot load image content. To load image content or obtain more powerful Web Support, use loadDataWithBaseURL.

2.    Many friends who use the loadData method may encounter garbled characters, which is caused by incorrect encoder settings. We know that the String type of data is mainly unicode encoding, and WebView is generally used to save resources is UTF-8 encoding, so we need to tell the method of transcoding when loadData. That is, tell it to convert the unicode-encoded content to the UTF-8-encoded content. Some friends set the encoding method when loadData, but it still displays garbled characters, because they also need to specify the encoding method for WebView's text encoding. Example:

              WebView wv = (WebView) findViewById (R. id. webview );

              String content = getUnicodeContent ();

              Wv. getSettings (). setdefatextextencodingnaMe ("UTF-8 ");

              Wv. loadData (content, "text/html", "UTF-8 ");

The Code is as follows:

WebSettings wSet = wView. getSettings ();
WSet. setJavaScriptEnabled (true );
// Supports scaling.
WSet. setBuiltInZoomControls (true );
// Supports image loading.
WSet. setBlockNetworkImage (false );
// Set to support caching
WSet. setAppCacheEnabled (true );
// Set the cache mode to default
WSet. setCacheMode (WebSettings. LOAD_DEFAULT );
WSet. setCacheMode (WebSettings. LOAD_DEFAULT );
Mt_rel = (RelativeLayout) findViewById (R. id. mt_rel );

// Create a WebViewClient object
WebViewClient wvc = new WebViewClient (){
@ Override
Public boolean shouldOverrideUrlLoading (WebView view, String url ){
// Use your own WebView component to respond to Url loading events, instead of loading pages using the default browser
WView. loadUrl (url );
// Remember to consume this event. Let me explain to a friend who doesn't know that the Android app returns True, which means that the event will be passed through bubbles. We call it consumption.
Return true;
}


@ Override
Public void onPageStarted (WebView view, String url, Bitmap favicon ){
Super. onPageStarted (view, url, favicon );
}


@ Override
Public void onPageFinished (WebView view, String url ){
// Toast. makeText (getApplicationContext (), "WebViewClient. onPageFinished", Toast. LENGTH_SHORT). show ();
Super. onPageFinished (view, url );
MCustomDialog. dismiss ();;
}




@ Override
Public void onLoadResource (WebView view, String url ){
// Toast. makeText (getApplicationContext (), "WebViewClient. onLoadResource", Toast. LENGTH_SHORT). show ();
Super. onLoadResource (view, url );
}
@ Override
Public void onReceivedError (WebView view, int errorCode, String description, String failingUrl ){
// Toast. makeText (getApplicationContext (), "WebViewClient. onReceivedError", Toast. LENGTH_SHORT). show ();
Super. onReceivedError (view, errorCode, description, failingUrl );
}
@ Override
Public void onScaleChanged (WebView view, float oldScale, float newScale ){
// Toast. makeText (getApplicationContext (), "WebViewClient. onScaleChanged", Toast. LENGTH_SHORT). show ();
Super. onScaleChanged (view, oldScale, newScale );
}
};

// Cache of webView: Click the return key

/**

* WebView's response to the Back button is to exit. If you want to exit at the first level, you need to listen to the Back button.

* Listen to the pop-up status of the button

* If the return value is true, it indicates that the request is blocked and no downstream request is sent.

*/

@ Override

Public boolean onKeyUp (int keyCode, KeyEvent event ){

Log. d (TAG, "onKeyUp keyCode:" + keyCode );

If (keyCode = KeyEvent. KEYCODE_BACK & mWebView. canGoBack ()){

MWebView. goBack ();

Return true;

}

Return super. onKeyUp (keyCode, event );

}



It can be seen that:

LoadData ("

It is best to use: LOadDataWithBaseUrl (); Method for loading.


Old yu always believes that:

Nothing can be done, just think of it.

Add your own blog address: http://blog.csdn.net/androidstarjack
You are welcome to join us in exploring and learning.


 


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.