Most visited

Recently visited

Added in API level 1

WebViewClient

public class WebViewClient
extends Object

java.lang.Object
   ↳ android.webkit.WebViewClient


Summary

Constants

int ERROR_AUTHENTICATION

User authentication failed on server

int ERROR_BAD_URL

Malformed URL

int ERROR_CONNECT

Failed to connect to the server

int ERROR_FAILED_SSL_HANDSHAKE

Failed to perform SSL handshake

int ERROR_FILE

Generic file error

int ERROR_FILE_NOT_FOUND

File not found

int ERROR_HOST_LOOKUP

Server or proxy hostname lookup failed

int ERROR_IO

Failed to read or write to the server

int ERROR_PROXY_AUTHENTICATION

User authentication failed on proxy

int ERROR_REDIRECT_LOOP

Too many redirects

int ERROR_TIMEOUT

Connection timed out

int ERROR_TOO_MANY_REQUESTS

Too many requests during this load

int ERROR_UNKNOWN

Generic error

int ERROR_UNSUPPORTED_AUTH_SCHEME

Unsupported authentication scheme (not basic or digest)

int ERROR_UNSUPPORTED_SCHEME

Unsupported URI scheme

Public constructors

WebViewClient()

Public methods

void doUpdateVisitedHistory(WebView view, String url, boolean isReload)

Notify the host application to update its visited links database.

void onFormResubmission(WebView view, Message dontResend, Message resend)

As the host application if the browser should resend data as the requested page was a result of a POST.

void onLoadResource(WebView view, String url)

Notify the host application that the WebView will load the resource specified by the given url.

void onPageCommitVisible(WebView view, String url)

Notify the host application that WebView content left over from previous page navigations will no longer be drawn.

void onPageFinished(WebView view, String url)

Notify the host application that a page has finished loading.

void onPageStarted(WebView view, String url, Bitmap favicon)

Notify the host application that a page has started loading.

void onReceivedClientCertRequest(WebView view, ClientCertRequest request)

Notify the host application to handle a SSL client certificate request.

void onReceivedError(WebView view, int errorCode, String description, String failingUrl)

This method was deprecated in API level 23. Use onReceivedError(WebView, WebResourceRequest, WebResourceError) instead.

void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)

Report web resource loading error to the host application.

void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm)

Notifies the host application that the WebView received an HTTP authentication request.

void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse)

Notify the host application that an HTTP error has been received from the server while loading a resource.

void onReceivedLoginRequest(WebView view, String realm, String account, String args)

Notify the host application that a request to automatically log in the user has been processed.

void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)

Notify the host application that an SSL error occurred while loading a resource.

void onScaleChanged(WebView view, float oldScale, float newScale)

Notify the host application that the scale applied to the WebView has changed.

void onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg)

This method was deprecated in API level 8. This method is no longer called. When the WebView encounters a redirect loop, it will cancel the load.

void onUnhandledKeyEvent(WebView view, KeyEvent event)

Notify the host application that a key was not handled by the WebView.

WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)

Notify the host application of a resource request and allow the application to return the data.

WebResourceResponse shouldInterceptRequest(WebView view, String url)

This method was deprecated in API level 21. Use shouldInterceptRequest(WebView, WebResourceRequest) instead.

boolean shouldOverrideKeyEvent(WebView view, KeyEvent event)

Give the host application a chance to handle the key event synchronously.

boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView.

boolean shouldOverrideUrlLoading(WebView view, String url)

This method was deprecated in API level 24. Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead.

Inherited methods

From class java.lang.Object

Constants

ERROR_AUTHENTICATION

Added in API level 5
int ERROR_AUTHENTICATION

User authentication failed on server

Constant Value: -4 (0xfffffffc)

ERROR_BAD_URL

Added in API level 5
int ERROR_BAD_URL

Malformed URL

Constant Value: -12 (0xfffffff4)

ERROR_CONNECT

Added in API level 5
int ERROR_CONNECT

Failed to connect to the server

Constant Value: -6 (0xfffffffa)

ERROR_FAILED_SSL_HANDSHAKE

Added in API level 5
int ERROR_FAILED_SSL_HANDSHAKE

Failed to perform SSL handshake

Constant Value: -11 (0xfffffff5)

ERROR_FILE

Added in API level 5
int ERROR_FILE

Generic file error

Constant Value: -13 (0xfffffff3)

ERROR_FILE_NOT_FOUND

Added in API level 5
int ERROR_FILE_NOT_FOUND

File not found

Constant Value: -14 (0xfffffff2)

ERROR_HOST_LOOKUP

Added in API level 5
int ERROR_HOST_LOOKUP

Server or proxy hostname lookup failed

Constant Value: -2 (0xfffffffe)

ERROR_IO

Added in API level 5
int ERROR_IO

Failed to read or write to the server

Constant Value: -7 (0xfffffff9)

ERROR_PROXY_AUTHENTICATION

Added in API level 5
int ERROR_PROXY_AUTHENTICATION

User authentication failed on proxy

Constant Value: -5 (0xfffffffb)

ERROR_REDIRECT_LOOP

Added in API level 5
int ERROR_REDIRECT_LOOP

Too many redirects

Constant Value: -9 (0xfffffff7)

ERROR_TIMEOUT

Added in API level 5
int ERROR_TIMEOUT

Connection timed out

Constant Value: -8 (0xfffffff8)

ERROR_TOO_MANY_REQUESTS

Added in API level 5
int ERROR_TOO_MANY_REQUESTS

Too many requests during this load

Constant Value: -15 (0xfffffff1)

ERROR_UNKNOWN

Added in API level 5
int ERROR_UNKNOWN

Generic error

Constant Value: -1 (0xffffffff)

ERROR_UNSUPPORTED_AUTH_SCHEME

Added in API level 5
int ERROR_UNSUPPORTED_AUTH_SCHEME

Unsupported authentication scheme (not basic or digest)

Constant Value: -3 (0xfffffffd)

ERROR_UNSUPPORTED_SCHEME

Added in API level 5
int ERROR_UNSUPPORTED_SCHEME

Unsupported URI scheme

Constant Value: -10 (0xfffffff6)

Public constructors

WebViewClient

Added in API level 1
WebViewClient ()

Public methods

doUpdateVisitedHistory

Added in API level 1
void doUpdateVisitedHistory (WebView view, 
                String url, 
                boolean isReload)

Notify the host application to update its visited links database.

Parameters
view WebView: The WebView that is initiating the callback.
url String: The url being visited.
isReload boolean: True if this url is being reloaded.

onFormResubmission

Added in API level 1
void onFormResubmission (WebView view, 
                Message dontResend, 
                Message resend)

As the host application if the browser should resend data as the requested page was a result of a POST. The default is to not resend the data.

Parameters
view WebView: The WebView that is initiating the callback.
dontResend Message: The message to send if the browser should not resend
resend Message: The message to send if the browser should resend data

onLoadResource

Added in API level 1
void onLoadResource (WebView view, 
                String url)

Notify the host application that the WebView will load the resource specified by the given url.

Parameters
view WebView: The WebView that is initiating the callback.
url String: The url of the resource the WebView will load.

onPageCommitVisible

Added in API level 23
void onPageCommitVisible (WebView view, 
                String url)

Notify the host application that WebView content left over from previous page navigations will no longer be drawn.

This callback can be used to determine the point at which it is safe to make a recycled WebView visible, ensuring that no stale content is shown. It is called at the earliest point at which it can be guaranteed that onDraw(Canvas) will no longer draw any content from previous navigations. The next draw will display either the background color of the WebView, or some of the contents of the newly loaded page.

This method is called when the body of the HTTP response has started loading, is reflected in the DOM, and will be visible in subsequent draws. This callback occurs early in the document loading process, and as such you should expect that linked resources (for example, css and images) may not be available.

For more fine-grained notification of visual state updates, see postVisualStateCallback(long, WebView.VisualStateCallback).

Please note that all the conditions and recommendations applicable to postVisualStateCallback(long, WebView.VisualStateCallback) also apply to this API.

This callback is only called for main frame navigations.

Parameters
view WebView: The WebView for which the navigation occurred.
url String: The URL corresponding to the page navigation that triggered this callback.

onPageFinished

Added in API level 1
void onPageFinished (WebView view, 
                String url)

Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).

Parameters
view WebView: The WebView that is initiating the callback.
url String: The url of the page.

onPageStarted

Added in API level 1
void onPageStarted (WebView view, 
                String url, 
                Bitmap favicon)

Notify the host application that a page has started loading. This method is called once for each main frame load so a page with iframes or framesets will call onPageStarted one time for the main frame. This also means that onPageStarted will not be called when the contents of an embedded frame changes, i.e. clicking a link whose target is an iframe, it will also not be called for fragment navigations (navigations to #fragment_id).

Parameters
view WebView: The WebView that is initiating the callback.
url String: The url to be loaded.
favicon Bitmap: The favicon for this page if it already exists in the database.

onReceivedClientCertRequest

Added in API level 21
void onReceivedClientCertRequest (WebView view, 
                ClientCertRequest request)

Notify the host application to handle a SSL client certificate request. The host application is responsible for showing the UI if desired and providing the keys. There are three ways to respond: proceed(), cancel() or ignore(). Webview stores the response in memory (for the life of the application) if proceed() or cancel() is called and does not call onReceivedClientCertRequest() again for the same host and port pair. Webview does not store the response if ignore() is called. Note that, multiple layers in chromium network stack might be caching the responses, so the behavior for ignore is only a best case effort. This method is called on the UI thread. During the callback, the connection is suspended. For most use cases, the application program should implement the KeyChainAliasCallback interface and pass it to choosePrivateKeyAlias(Activity, KeyChainAliasCallback, String[], Principal[], Uri, String) to start an activity for the user to choose the proper alias. The keychain activity will provide the alias through the callback method in the implemented interface. Next the application should create an async task to call getPrivateKey(Context, String) to receive the key. An example implementation of client certificates can be seen at AOSP Browser The default behavior is to cancel, returning no client certificate.

Parameters
view WebView: The WebView that is initiating the callback
request ClientCertRequest: An instance of a ClientCertRequest

onReceivedError

Added in API level 1
void onReceivedError (WebView view, 
                int errorCode, 
                String description, 
                String failingUrl)

This method was deprecated in API level 23.
Use onReceivedError(WebView, WebResourceRequest, WebResourceError) instead.

Report an error to the host application. These errors are unrecoverable (i.e. the main resource is unavailable). The errorCode parameter corresponds to one of the ERROR_* constants.

Parameters
view WebView: The WebView that is initiating the callback.
errorCode int: The error code corresponding to an ERROR_* value.
description String: A String describing the error.
failingUrl String: The url that failed to load.

onReceivedError

Added in API level 23
void onReceivedError (WebView view, 
                WebResourceRequest request, 
                WebResourceError error)

Report web resource loading error to the host application. These errors usually indicate inability to connect to the server. Note that unlike the deprecated version of the callback, the new version will be called for any resource (iframe, image, etc), not just for the main page. Thus, it is recommended to perform minimum required work in this callback.

Parameters
view WebView: The WebView that is initiating the callback.
request WebResourceRequest: The originating request.
error WebResourceError: Information about the error occured.

onReceivedHttpAuthRequest

Added in API level 1
void onReceivedHttpAuthRequest (WebView view, 
                HttpAuthHandler handler, 
                String host, 
                String realm)

Notifies the host application that the WebView received an HTTP authentication request. The host application can use the supplied HttpAuthHandler to set the WebView's response to the request. The default behavior is to cancel the request.

Parameters
view WebView: the WebView that is initiating the callback
handler HttpAuthHandler: the HttpAuthHandler used to set the WebView's response
host String: the host requiring authentication
realm String: the realm for which authentication is required

See also:

onReceivedHttpError

Added in API level 23
void onReceivedHttpError (WebView view, 
                WebResourceRequest request, 
                WebResourceResponse errorResponse)

Notify the host application that an HTTP error has been received from the server while loading a resource. HTTP errors have status codes >= 400. This callback will be called for any resource (iframe, image, etc), not just for the main page. Thus, it is recommended to perform minimum required work in this callback. Note that the content of the server response may not be provided within the errorResponse parameter.

Parameters
view WebView: The WebView that is initiating the callback.
request WebResourceRequest: The originating request.
errorResponse WebResourceResponse: Information about the error occured.

onReceivedLoginRequest

Added in API level 12
void onReceivedLoginRequest (WebView view, 
                String realm, 
                String account, 
                String args)

Notify the host application that a request to automatically log in the user has been processed.

Parameters
view WebView: The WebView requesting the login.
realm String: The account realm used to look up accounts.
account String: An optional account. If not null, the account should be checked against accounts on the device. If it is a valid account, it should be used to log in the user.
args String: Authenticator specific arguments used to log in the user.

onReceivedSslError

Added in API level 8
void onReceivedSslError (WebView view, 
                SslErrorHandler handler, 
                SslError error)

Notify the host application that an SSL error occurred while loading a resource. The host application must call either handler.cancel() or handler.proceed(). Note that the decision may be retained for use in response to future SSL errors. The default behavior is to cancel the load.

Parameters
view WebView: The WebView that is initiating the callback.
handler SslErrorHandler: An SslErrorHandler object that will handle the user's response.
error SslError: The SSL error object.

onScaleChanged

Added in API level 1
void onScaleChanged (WebView view, 
                float oldScale, 
                float newScale)

Notify the host application that the scale applied to the WebView has changed.

Parameters
view WebView: The WebView that is initiating the callback.
oldScale float: The old scale factor
newScale float: The new scale factor

onTooManyRedirects

Added in API level 1
void onTooManyRedirects (WebView view, 
                Message cancelMsg, 
                Message continueMsg)

This method was deprecated in API level 8.
This method is no longer called. When the WebView encounters a redirect loop, it will cancel the load.

Notify the host application that there have been an excessive number of HTTP redirects. As the host application if it would like to continue trying to load the resource. The default behavior is to send the cancel message.

Parameters
view WebView: The WebView that is initiating the callback.
cancelMsg Message: The message to send if the host wants to cancel
continueMsg Message: The message to send if the host wants to continue

onUnhandledKeyEvent

Added in API level 1
void onUnhandledKeyEvent (WebView view, 
                KeyEvent event)

Notify the host application that a key was not handled by the WebView. Except system keys, WebView always consumes the keys in the normal flow or if shouldOverrideKeyEvent returns true. This is called asynchronously from where the key is dispatched. It gives the host application a chance to handle the unhandled key events.

Parameters
view WebView: The WebView that is initiating the callback.
event KeyEvent: The key event.

shouldInterceptRequest

Added in API level 21
WebResourceResponse shouldInterceptRequest (WebView view, 
                WebResourceRequest request)

Notify the host application of a resource request and allow the application to return the data. If the return value is null, the WebView will continue to load the resource as usual. Otherwise, the return response and data will be used. NOTE: This method is called on a thread other than the UI thread so clients should exercise caution when accessing private data or the view system.

Parameters
view WebView: The WebView that is requesting the resource.
request WebResourceRequest: Object containing the details of the request.
Returns
WebResourceResponse A WebResourceResponse containing the response information or null if the WebView should load the resource itself.

shouldInterceptRequest

Added in API level 11
WebResourceResponse shouldInterceptRequest (WebView view, 
                String url)

This method was deprecated in API level 21.
Use shouldInterceptRequest(WebView, WebResourceRequest) instead.

Notify the host application of a resource request and allow the application to return the data. If the return value is null, the WebView will continue to load the resource as usual. Otherwise, the return response and data will be used. NOTE: This method is called on a thread other than the UI thread so clients should exercise caution when accessing private data or the view system.

Parameters
view WebView: The WebView that is requesting the resource.
url String: The raw url of the resource.
Returns
WebResourceResponse A WebResourceResponse containing the response information or null if the WebView should load the resource itself.

shouldOverrideKeyEvent

Added in API level 1
boolean shouldOverrideKeyEvent (WebView view, 
                KeyEvent event)

Give the host application a chance to handle the key event synchronously. e.g. menu shortcut key events need to be filtered this way. If return true, WebView will not handle the key event. If return false, WebView will always handle the key event, so none of the super in the view chain will see the key event. The default behavior returns false.

Parameters
view WebView: The WebView that is initiating the callback.
event KeyEvent: The key event.
Returns
boolean True if the host application wants to handle the key event itself, otherwise return false

shouldOverrideUrlLoading

Added in API level 24
boolean shouldOverrideUrlLoading (WebView view, 
                WebResourceRequest request)

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url.

Notes:

  • This method is not called for requests using the POST "method".
  • This method is also called for subframes with non-http schemes, thus it is strongly disadvised to unconditionally call loadUrl(String) with the request's url from inside the method and then return true, as this will make WebView to attempt loading a non-http url, and thus fail.

Parameters
view WebView: The WebView that is initiating the callback.
request WebResourceRequest: Object containing the details of the request.
Returns
boolean True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.

shouldOverrideUrlLoading

Added in API level 1
boolean shouldOverrideUrlLoading (WebView view, 
                String url)

This method was deprecated in API level 24.
Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead.

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".

Parameters
view WebView: The WebView that is initiating the callback.
url String: The url to be loaded.
Returns
boolean True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.

Hooray!