Reprint Please specify the source http://blog.csdn.net/typename/article/details/39495409powered by Meichal 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 display the Web page loading progress, page loading errors and so on events. 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 the Web page loading various stages of the notification, such as Web page start loading onpagestarted, page end loading onpagefinished, etc. webchromeclient mainly provide the content of the data provided during the Web page loading, such as the return page Title,favicon.
The basic use of 1.WebViewClient creates a webviewclient instance and sets it to the WebView object, which is referenced in the following code:
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 details 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 load, if the application takes over, and return true means that the main program takes over the page load, if return false let WebView handle it by itself.
Parameter 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, if we find a link to a market, we can go directly to the marketplace, or to other apps.
public void onpagestarted (WebView view, String URL, Bitmap favicon)
When the kernel starts to load the accessed URL, the application is notified that the function for each main frame is only called once, the page contains an IFRAME, or framesets does not call another onpagestarted, when the frame embedded within the page Onpagestarted is not called when a change occurs.
Parameter description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. The @param URL is the Url@param that will be loaded favicon if the favicon is already stored in the local database, the favicon of the Web page is returned, otherwise null is returned.
Tips:(1) iframe may be a lot of people do not know what the meaning, here I explain the iframe we loaded a sheet, there are many links below, 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 in the process of Web page loading, which of these two functions is called first. when we reload a URL by Loadurl, we call onpagestarted again and call Shouldoverrideurlloading, when we click on a link in the open URL, This will call Shouldoverrideurlloading before calling onpagestarted. However, shouldoverrideurlloading is not necessarily called every time, but only when it is needed.
public void onpagefinished (WebView view, String URL)
When the kernel loads the current page, it notifies our application that the function is called only in the case of main frame, when the function is called, the rendered picture is not updated, and if you need to get notification of the new image you can use @link Webview.picturelistener#onnewpicture.
Parameter 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 to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. URL resource that @param URL is about to load
Public Webresourceresponse shouldinterceptrequest (WebView view, String URL)
Notifies the application that the kernel is about to load URL-developed resources, the application can return local resources to the kernel, and if the returned data is processed locally, the kernel does not fetch data from the network.
Parameter 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 returns Webresourceresponse contains data object, or returns null
TipsThis callback does not necessarily work on the UI thread, so we need to be aware of the actions associated with view or private data here. If we need to change the background of the page, or need to implement the page color customization needs, can be processed at this callback time.
public void Onreceivederror (WebView view, int errorCode, string description, String failingurl)
When a browser accesses a URL that has been made, it notifies us of the application, such as a network error.
Parameter 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 describes the wrong information @param failingurl the URL of the current access failure, note that it is not necessarily our main URL
Tips in Onreceiveerror We can customize the page's error page.
public void Onformresubmission (WebView view, message dontresend, message Resend)
If the browser needs to resend the POST request, it can be handled by this time. The default is to not resend the data. Parameter description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. @param dontresent This parameter can be used when the browser does not need to resend the data. @param resent this parameter can be used when the browser needs to resend the data.
public void Doupdatevisitedhistory (WebView view, String URL, boolean isreload)
The notification application can store the current URL in the database, meaning that the current access URL is in effect and is logged in the kernel. This function is only called once during page loading. Note that the page forward and backward does not callback this function.
Parameter description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview. The URL currently being accessed by the @param url @ param isreload If 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 load 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 to receive 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, and if the next time there is the same error, the kernel executes the results of the previous selection directly.
public void Onreceivedhttpauthrequest (WebView view, Httpauthhandler handler, string host, String realm)
Notifies the application that WebView receives an HTTP AUTH request, and the application can use supplied to set the WebView response request. The default behavior is to cancel this request. Parameter 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. If return True,webview does not handle the event, WebView will always handle the event if it returns false, so there is no parent class on the view chain that can respond to this event. The default behavior is return false; parameter description: @param view receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above , that is the webview. @param event Keyboard Events name @return If true, the application processes the time and returns False WebView processing.
public void onscalechanged (WebView view, float Oldscale, float newscale)
Notifies the application that WebView is to be scale. Applications can handle change events, such as adjusting the adaptation screen.
public void Onreceivedloginrequest (WebView view, String realm, string account, String args)
The notification application has an automatic login account process parameter description: The domain name of the Webview@param realm account that @param view request to log in to find the account. @param accounts An optional account, if NULL is required to check with the local account, and if it is an available account, provide login. Login user @param args to validate parameters
3.WebChromeClient Basic Use
4. Webchromeclient API details To create an Webchromeclient instance and set it to WebView object, the code reference is as follows:
public void onprogresschanged (WebView view, int newprogress)
Notifies the application of the current page load progress. Parameter 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 document changes, the application parameter description is notified: @param view receives the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()), which is the webview. @param title Document New Titletips This function call time is uncertain, it may be very early, may be very late, depending on the Web page to set the title in what position, most Web pages generally 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 to receive 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
Parameter 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 if precomposed is true Touch-icon is a pre-created tip if the application requires this icon, you can use this URL get icon.
public void Onshowcustomview (view view, Customviewcallback callback)
Notifies the application that WebView needs to display a custom view, which is primarily used in the video full screen html5video support. Parameter description: @param view is about to display the View@param callback when view needs 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 window, if our application takes over this request, must return true, and create a new WebView to host the main window. If the application does not process, you need to return FALSE, and the default behavior is the same as returning false. Parameter description: Webview@param isusergesture for creating a new window @param view request If true, the description is from the user to clean up the operation behavior, such as the user click the link @param isdialog true The new window created by the request must be a dialog, not a full-screen window. @param resultmsg A message needs to be sent when WebView is created. WebView.WebViewTransport.setWebView (WebView) Tips specific 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 the background 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, and if the application returns True the kernel considers the application to handle this message, returning false to the kernel itself. Parameter description: @param view to receive the instance of Webviewclient, see Webview.setwebviewclient (New Myandroidwebviewclient ()) above, that is the webview.
@param URL Current request popup JavaScript dialog webview loaded URL address. @param message pops up the content information @result is used to respond to the user's handling.
TipsIf we apply takeover processing, we must give results of result, result.cancel,result.comfirm must call it, 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. Parameter Description 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 if 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, and if the client returns true, the kernel considers the client to provide a dialog box. The default behavior is return FALSE. The parameter 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 to not increase the database quota. Parameter description:
@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, which is the size of the byte unit Bytes@param estimateddatabasesize the data to reach the baseline Bytes@param Totalquota Total database quota size Bytes@param q Uotaupdater the object that updates the database quota, you can use Quotaupdater.updatequota (Newquota) and configure the 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 allows positioning.
Use of Geolocationpermissions.callback
public void Invoke (String origin, Boolean allow, Boolean retain);
Parameter description: @param origin permission set source address @param allow to locate @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 be called back to this method, such as we need to upload images, request photos, mail attachments upload and so on. If you do not implement this private API, none of the above requests will be executed.
There are questions to welcome the discussion.
Android WebView Development Details (ii)