Android WebView Development Specific Explanation (II)

Source: Internet
Author: User

Reprint Please specify the source http://blog.csdn.net/typename/article/details/39495409powered by Miechal Zhao
Overview:

Android WebView as a vector control for hosting web pages, he will generate some events during the Web page display and callback to our application so that we can do what the application wants to do during the page loading process. For example, the client needs to show the progress of the page loading, the page loading error occurred, and so on.

WebView provides two event callback classes to the application layer, respectively, for webviewclient,webchromeclient developers to inherit these two classes and take over the corresponding event handling.

Webviewclient mainly provides notifications of the various stages of Web page loading, for example, when the Web page starts to load onpagestarted. The end of the page is loaded into onpagefinished and so on, webchromeclient mainly provides the content of the data provided in the Web page loading process, such as returning the Title,favicon of the webpage.

The basic use of 1.WebViewClient creates a webviewclient instance and sets it to the WebView object, with detailed code references such as the following:
Class Myandroidwebviewclient extends Webviewclient {     @Override public    void onpagestarted (WebView view, String URL, Bitmap favicon) {       //TODO    }    @Override public    void onpagefinished (WebView view, String URL) {      // TODO    }
}webview.setwebviewclient (New Myandroidwebviewclient ());

2.WebViewClient API explained in detail 1) page Loading timing section
public boolean shouldoverrideurlloading (WebView view, String URL)
When the loaded Web page needs to redirect, it will call back this function tells us whether the application needs to take over the control page loading. Assuming that the application takes over, and that return true means that the main program takes over the page loading, assume that the return false lets WebView handle itself.


Description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. The @param URL will be loaded Url@return True the current application will handle the URL itself and return false to not handle it.


Tips(1) This callback is not notified when the request is in the "POST" mode.

(2) When the address we visit needs to be handled by our application, we can intercept it here, for example, we find that we jump to a market link, then we can go directly to the marketplace, or other apps.


public void onpagestarted (WebView view, String URL, Bitmap favicon)
When the kernel starts loading the URL of the interview, the application is notified that the function for each main frame is only called once. The page includes an IFRAME or framesets does not call another onpagestarted, and onpagestarted is not called when the frame inside the page changes.



Parameter description: @param view receives the instance of Webviewclient. See Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. The @param URL is the Url@param favicon that will be loaded if the favicon is already stored in the local database, the favicon of this web page is returned. Otherwise, the return is null.
Tips:(1) iframe may be a lot of people do not know what meaning, here I explain. IFrame we loaded one, the following there are very many links, we casually click on a link is the current host an IFRAME.(2) There is a problem that may be confusing to developers, onpagestarted and shouldoverrideurlloading the two functions are called first in the Web page loading process. when we load a URL again by Loadurl, this time we call onpagestarted and then call Shouldoverrideurlloading, when we click on a link in the open URL, This will call Shouldoverrideurlloading before calling onpagestarted.

Just shouldoverrideurlloading is not necessarily called every time. It is called only when there is a need.


public void onpagefinished (WebView view, String URL)

Our application is notified when the kernel loads the current page. This function is called only in the case of main frame, when the function is called, the rendered picture is not updated. Suppose you need to be notified of new images to use @link webview.picturelistener#onnewpicture.


Description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. @param url is the URL that will be loaded
public void Onloadresource (WebView view, String URL)
Notifies the application that WebView is about to load URL-developed resources
Parameter description: @param view receives the instance of Webviewclient. See Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. URL resource to be loaded @param URL
Public Webresourceresponse shouldinterceptrequest (WebView view,            String URL)
Notifies the application that the kernel is about to load a URL-established resource, and the application can return the local resource to the kernel. If the returned data is processed locally, the kernel does not fetch data from the network.
Description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. @param URL Raw URL for resource @return return webresourceresponse include data object, or return null
TipsThis callback does not necessarily run on the UI thread, so we need to be careful about manipulating view or private data-related actions here. Let's say we need to change the background of the page, or need to implement the page color customization. Able to handle at this callback time.
public void Onreceivederror (WebView view, int errorCode,            string description, String failingurl)
The application is notified when a browser visits a URL error that has been made. For example, a network error.
Description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. @param errorCode Error number can find the corresponding error name in the webviewclient.error_*. @param description Descriptive error information @param failingurl current access failed URL, note is not necessarily our main URL
Tips in Onreceiveerror We are able to define our own page's error page.
public void Onformresubmission (WebView view, message dontresend,            message Resend)
Let's say that the browser needs to send a POST request again, which can be handled by this time. The default is to send data again and again.

Parameter description: @param view receives the instance of Webviewclient. See Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview.

@param dontresent When the browser does not need to send data again. Be able to use this number of parameters.

@param resent Use this parameter when the browser needs to send data again.
public void Doupdatevisitedhistory (WebView view, String URL,            boolean isreload)

The notification application is able to store the current URL in the database, meaning that the current access URL is in effect and is recorded in the kernel.

This function is only called once during page loading. Note that the page forward and backward does not callback this function.


Description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. The URL currently being visited by the @param URL @ param Isreload assumes true this is the URL that is being reload

public void Onreceivedsslerror (WebView view, Sslerrorhandler handler,            sslerror error)

This method is called when an SSL error is found during a Web page loading resource. Our application must respond by canceling the request Handler.cancel () or continuing to request handler.proceed (); The default behavior of the kernel is handler.cancel ();
Parameter description: @param view receives the instance of Webviewclient. See Webview.setwebviewclient (New Myandroidwebviewclient ()) above. That is the webview. @param handler the object to process the user request. @param Error SSL Error object
TipsThe kernel remembers this selection, assuming that the next time there is the same error, the kernel will run the results of the previous selection directly.
public void Onreceivedhttpauthrequest (WebView view,            Httpauthhandler handler, string host, String realm)

Notifies the application that WebView received an HTTP AUTH request that the application can use supplied to set the WebView response request.

The default behavior is to cancel this request.

Description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview.

@param handler object used to respond to WebView requests @param host request authentication Host@param realm earnestly requests the domain
Provides application synchronization an opportunity to handle key events, menu shortcuts need to be filtered out.

Assuming that the return True,webview does not handle the event, assuming that the event is returned false, WebView will always handle it, so no parent class on the view chain can respond to this event. The default behavior is return false;

Parameter description: @param view receives the instance of Webviewclient. See Webview.setwebviewclient (New Myandroidwebviewclient ()) above. That is the webview.

@param event Keyboard Events name @return is assumed to return true, the application processes the time, and returns false with WebView processing.
public void onscalechanged (WebView view, float Oldscale, float newscale)
Notifies the application that WebView is to be scale. The application can handle the change event. For example, adjust the adaptation screen.

public void Onreceivedloginrequest (WebView view, String realm,            string account, String args)

Notifies the application that it has an account that is actively logged in. Description: The domain name of the Webview@param realm account that @param view request to log in to find the account. An optional account for @param accounts, assuming that null is required to check with the local account, provided that it is an available account, provide login. @param args to verify the number of login users
3.WebChromeClient Basic Use
4. The Webchromeclient API explains in detail the creation of the Webchromeclient instance and sets it into the WebView object. Detailed code references such as the following:

public void onprogresschanged (WebView view, int newprogress)
Notifies the application of the current page loading progress. Description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview.


public void Onreceivedtitle (WebView view, String title)

When the title of the document changes, the application description is notified: The @param view receives the instance of Webviewclient, and the previous view webview.setwebviewclient (new Myandroidwebviewclient ()), which is the webview. @param title Document New Titletips This function call time is uncertain. It's probably very early. It's probably very late. Depending on where the page is located, most Web pages usually set the title to the front of the page. So a lot of things get back to this function earlier.
public void Onreceivedicon (WebView view, Bitmap icon)

This function is recalled when the current page has a new favicon.


Parameter description: @param view receives the instance of Webviewclient. See Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview.

Favicon of the current page of the @param icon
public void Onreceivedtouchiconurl (WebView view, String URL,            boolean precomposed)


Notifies the application of the Apple-touch-icon URL
Description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. The service-side address of the @param URL Apple-touch-icon @param precomposed assumes that precomposed is true Touch-icon is a pre-created tips hypothesis that the application needs this icon to be able to pass this URL get icon.
public void Onshowcustomview (view view, Customviewcallback callback)

Notification application WebView need to display a custom view, mainly used in video full screen html5video support. Description: @param view is about to display the View@param callback when the view requires dismiss use this object for callback notification.

public void Onhidecustomview ()
Exit Video Notifications
public boolean Oncreatewindow (WebView view, Boolean Isdialog,            boolean isusergesture, Message resultmsg)
Request to create a new form. Suppose our application takes over this request, must return true, and create a new WebView to host the main form. If the application does not process, you need to return FALSE. The default behavior is the same as returning false. Parameter description: The Webview@param isusergesture hypothesis is true @param view request to create a new form. The description is from the user to clean up the operation behavior. For example, the user clicks the link @param isdialog true to create a new form that must be a dialog, not a full-screen form. @param resultmsg you need to send a message when WebView is created. WebView.WebViewTransport.setWebView (WebView) Tips Detailed sample examples are as follows:
    private void CreateWindow (Final Message msg) {Webview.webviewtransport transport = (webview.webviewtransport) msg.obj; Final Tab newTab = Mwebviewcontroller.opentab (null, tab.this, true,true); Transport.setwebview (Newtab.getwebview ()); Msg.sendtotarget ();    }

public void Onrequestfocus (WebView view)
WebView request to get focus. This is mainly the current webview is not the foreground state. is backstage webview.
public void Onclosewindow (WebView window)

Notifies the application to remove the WebView passed over from the view tree.
public boolean Onjsalert (WebView view, string URL, String message,            jsresult result)

Notifies the application that the JavaScript Alert dialog box is displayed, assuming the application returns true if the kernel feels that the application is handling this message, returns False, and the kernel handles itself. Description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview.
@param URL The current request pops up the JavaScript dialog box webview the loaded URL address. @param message pops up the content information @result is used to respond to the user's handling.
TipsAssuming that we apply the takeover process, we must give results of result. Result.cancel,result.comfirm must be called after, otherwise the kernel will hang.
public boolean onjsconfirm (WebView view, string URL, String message,            jsresult result)

Notifies the application that the Confirm dialog box is available. Description of the reference ditto Onjsalert
public boolean onjsprompt (WebView view, string URL, String message,            string defaultvalue, jspromptresult result)

Notifies the application that a prompt dialog box is displayed.

Tips must call the Result.confirm method to assume that the application takes over this method.
public boolean onjsbeforeunload (WebView view, string URL, String message,            jsresult result)
The notification application displays a dialog box that lets the user choose whether to leave the current page, which is the onbeforeunload event in JavaScript. Assuming that the client returns true, the kernel will feel that the client provides a dialog box. The default behavior is return FALSE. The reference description is the same as the Onjsalert () described earlier.


public void Onexceededdatabasequota (string url, string databaseidentifier,            long quota, long estimateddatabasesize, Long Totalquota,            webstorage.quotaupdater quotaupdater)

Notifies the application that the WebView kernel Web SQL database exceeds the quota and requests that the database disk quota be enlarged. The default behavior is that database quotas are not added. Description of the parameters:
@param URL to trigger this database quota URL address @param databaseidentifierIndicates the identity in which the database exceeded the quota. @param quota the size of the original database quota. is the byte unit bytes@param estimateddatabasesize the data size to reach the baseline Bytes@param Totalquota the total database quota size Bytes@param quotaupdater Update the database quota object, Ability to use Quotaupdater.updatequota (Newquota) and configure new database quota size.


public void Onreachedmaxappcachesize (long requiredstorage, long quota,            webstorage.quotaupdater quotaupdater)

Notifies the application that the kernel has reached the maximum appcache. AppCache is a data processing standard for HTML5 for offline.
public void Ongeolocationpermissionsshowprompt (String origin,            geolocationpermissions.callback Callback)

Whether the current page request agrees to be positioned.


Use of Geolocationpermissions.callback

public void Invoke (String origin, Boolean allow, Boolean retain);

Parameter description: @param origin permission set source address @param allow to agree to @retain whether the current selection is to be remembered by the kernel.
public void Ongeolocationpermissionshideprompt ()

public void Openfilechooser (valuecallback<uri> uploadfile, String accepttype, string capture)
This callback is a private callback. When the page needs to request to open the system's file selector, it will callback this method, for example, we need to upload images. Request a picture. The attachment upload of the message and so on.

Assume that this private API is not implemented. None of the above requests will be run.
There are questions to welcome the discussion.

Android WebView Development Specific Explanation (II)

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.