WebViewCompat

public class WebViewCompat
extends Object

java.lang.Object
   ↳ androidx.webkit.WebViewCompat


Compatibility version of WebView

Summary

Nested classes

interface WebViewCompat.VisualStateCallback

Callback interface supplied to WebViewCompat.postVisualStateCallback(WebView, long, WebViewCompat.VisualStateCallback) for receiving notifications about the visual state. 

interface WebViewCompat.WebMessageListener

This listener receives messages sent on the JavaScript object which was injected by WebViewCompat.addWebMessageListener(WebView, String, Set, WebViewCompat.WebMessageListener)

Public methods

static void addWebMessageListener(WebView webView, String jsObjectName, Set<String> allowedOriginRules, WebViewCompat.WebMessageListener listener)

Adds a WebViewCompat.WebMessageListener to the WebView and injects a JavaScript object into each frame that the WebViewCompat.WebMessageListener will listen on.

static WebMessagePortCompat[] createWebMessageChannel(WebView webview)

Creates a message channel to communicate with JS and returns the message ports that represent the endpoints of this message channel.

static PackageInfo getCurrentWebViewPackage(Context context)

If WebView has already been loaded into the current process this method will return the package that was used to load it.

static Uri getSafeBrowsingPrivacyPolicyUrl()

Returns a URL pointing to the privacy policy for Safe Browsing reporting.

static WebChromeClient getWebChromeClient(WebView webview)

Gets the WebChromeClient.

static WebViewClient getWebViewClient(WebView webview)

Gets the WebViewClient for the WebView argument.

static WebViewRenderProcess getWebViewRenderProcess(WebView webview)

Gets the WebView renderer associated with this WebView.

static WebViewRenderProcessClient getWebViewRenderProcessClient(WebView webview)

Gets the renderer client object associated with this WebView.

static boolean isMultiProcessEnabled()

Returns true if WebView is running in multi process mode.

static void postVisualStateCallback(WebView webview, long requestId, WebViewCompat.VisualStateCallback callback)

Posts a WebViewCompat.VisualStateCallback, which will be called when the current state of the WebView is ready to be drawn.

static void postWebMessage(WebView webview, WebMessageCompat message, Uri targetOrigin)

Post a message to main frame.

static void removeWebMessageListener(WebView webview, String jsObjectName)

Removes the WebMessageListener associated with jsObjectName.

static void setSafeBrowsingAllowlist(Set<String> hosts, ValueCallback<Boolean> callback)

Configures a set of hosts (domain names/IP addresses) that are exempt from SafeBrowsing checks.

static void setSafeBrowsingWhitelist(List<String> hosts, ValueCallback<Boolean> callback)

This method is deprecated. Please use setSafeBrowsingAllowlist(Set, ValueCallback) instead.

static void setWebViewRenderProcessClient(WebView webview, Executor executor, WebViewRenderProcessClient webViewRenderProcessClient)

Sets the renderer client object associated with this WebView.

static void setWebViewRenderProcessClient(WebView webview, WebViewRenderProcessClient webViewRenderProcessClient)

Sets the renderer client object associated with this WebView.

static void startSafeBrowsing(Context context, ValueCallback<Boolean> callback)

Starts Safe Browsing initialization.

Inherited methods

Public methods

addWebMessageListener

public static void addWebMessageListener (WebView webView, 
                String jsObjectName, 
                Set<String> allowedOriginRules, 
                WebViewCompat.WebMessageListener listener)

Adds a WebViewCompat.WebMessageListener to the WebView and injects a JavaScript object into each frame that the WebViewCompat.WebMessageListener will listen on.

The injected JavaScript object will be named jsObjectName in the global scope. This will inject the JavaScript object in any frame whose origin matches allowedOriginRules for every navigation after this call, and the JavaScript object will be available immediately when the page begins to load.

Each allowedOriginRules entry must follow the format SCHEME "://" [ HOSTNAME_PATTERN [ ":" PORT ] ], each part is explained in the below table:

RuleDescriptionExample