The difference between webviewclient and webchromeclient

Source: Internet
Author: User

Android Application Development may use the WebView component, the use of the process may be exposed to webviewclient and webchromeclient, then what is the difference between the two classes?

webviewclient mainly helps WebView to handle various notifications, request events, such as:

Onloadresource,onpagestart,onpagefinish,onreceiveerror,onreceivedhttpauthrequest and so on.

Webview1.setwebviewclient (New Webviewclient () {public void Onreceivederror (WebView view, int errorCode, String Description, String failingurl) {//handle the Errortoast.maketext (Getapplicationcontext (), "Network connection failed, please connect to network. ", Toast.length_short). Show ();};}); Webview1.getsettings (). Setdefaulttextencodingname ("UTF-8"); Webview1.loadurl ("");


webchromeclient main Auxiliary webview Processing JavaScript dialog box, website icon, website title, loading progress, such as:

Onclosewindow (off WebView), Oncreatewindow (), Onjsalert (WebView on alert invalid, need to customize Webchromeclient processing popup), Onjsprompt, Onjsconfirm,onprogresschanged,onreceivedicon,onreceivedtitle and so on.

For example, you can add a progress bar to make the interface friendlier Webview1.setwebchromeclient (new Webchromeclient () {public    void onprogresschanged (WebView view, int progress) {         setprogress (progress *), if (progress = =) {         imageview1.setvisibility (view.gone);         Tv1.setvisibility (view.gone);         Pb1.setvisibility (View.gone); Fy1.setvisibility (View.gone); } } } );


It seems that they have a lot of different, actual use, if your webview is only used to deal with some HTML page content, only with webviewclient on the line, if you need more rich processing effect, such as JS, progress bar, etc., it is necessary to use webchromeclient.

More often, you can do this:

WebView webview;webview= (WebView) Findviewbyid (R.id.webview); Webview.setwebchromeclient (new Webchromeclient ()); Webview.setwebviewclient (New Webviewclient ()); Webview.getsettings (). Setjavascriptenabled (True); WEBVIEW.LOADURL ( URL);

Of course, some of the more exciting things you need to add yourself

The difference between webviewclient and webchromeclient

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.