WebViewCompat


public class WebViewCompat


Compatibility version of android.webkit.WebView

Summary

Nested types

Denotes that the startUpWebView API surface is experimental.

Denotes that the WebViewCompat#setShouldCacheProvider API surface is experimental.

Denotes that the WebViewCompat#saveState API surface is experimental.

Denotes that the PrerenderUrl API surface is experimental.

Callback interface supplied to postVisualStateCallback for receiving notifications about the visual state.

This listener receives messages sent on the JavaScript object which was injected by addWebMessageListener.

Callback interface for startUpWebView.

Public methods

static @NonNull ScriptHandler
@UiThread
@RequiresFeature(name = WebViewFeature.DOCUMENT_START_SCRIPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
addDocumentStartJavaScript(
    @NonNull WebView webview,
    @NonNull String script,
    @NonNull Set<String> allowedOriginRules
)

Adds a JavaScript script to the WebView which will be executed in any frame whose origin matches allowedOriginRules when the document begins to load.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_LISTENER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
addWebMessageListener(
    @NonNull WebView webView,
    @NonNull String jsObjectName,
    @NonNull Set<String> allowedOriginRules,
    @NonNull WebViewCompat.WebMessageListener listener
)

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

static @NonNull WebMessagePortCompat[]
@UiThread
@RequiresFeature(name = WebViewFeature.CREATE_WEB_MESSAGE_CHANNEL, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
createWebMessageChannel(@NonNull WebView webview)

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

static @Nullable PackageInfo

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

static @NonNull Profile
@UiThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getProfile(@NonNull WebView webView)

Gets the Profile associated with this WebView.

static @NonNull Uri
@AnyThread
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_PRIVACY_POLICY_URL, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getSafeBrowsingPrivacyPolicyUrl()

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

static @NonNull String
@AnyThread
@RequiresFeature(name = WebViewFeature.GET_VARIATIONS_HEADER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getVariationsHeader()

Gets the WebView variations encoded to be used as the X-Client-Data HTTP header.

static @Nullable WebChromeClient
@UiThread
@RequiresFeature(name = WebViewFeature.GET_WEB_CHROME_CLIENT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebChromeClient(@NonNull WebView webview)

Gets the WebChromeClient.

static @NonNull WebNavigationClient

Gets the WebNavigationClient currently set for the given WebView.

static @NonNull WebViewClient
@UiThread
@RequiresFeature(name = WebViewFeature.GET_WEB_VIEW_CLIENT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebViewClient(@NonNull WebView webview)

Gets the WebViewClient for the WebView argument.

static @Nullable WebViewRenderProcess
@UiThread
@RequiresFeature(name = WebViewFeature.GET_WEB_VIEW_RENDERER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebViewRenderProcess(@NonNull WebView webview)

Gets the WebView renderer associated with this WebView.

static @Nullable WebViewRenderProcessClient
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebViewRenderProcessClient(@NonNull WebView webview)

Gets the renderer client object associated with this WebView.

static boolean
@UiThread
@RequiresFeature(name = WebViewFeature.MUTE_AUDIO, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
isAudioMuted(@NonNull WebView webView)

Returns whether this WebView is muted.

static boolean
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
isMultiProcessEnabled()

Returns true if WebView is running in multi process mode.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.VISUAL_STATE_CALLBACK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
postVisualStateCallback(
    @NonNull WebView webview,
    long requestId,
    @NonNull WebViewCompat.VisualStateCallback callback
)

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

static void
@UiThread
@RequiresFeature(name = WebViewFeature.POST_WEB_MESSAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
postWebMessage(
    @NonNull WebView webview,
    @NonNull WebMessageCompat message,
    @NonNull Uri targetOrigin
)

Post a message to main frame.

static void
@RequiresFeature(name = WebViewFeature.PRERENDER_WITH_URL, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@WebViewCompat.ExperimentalUrlPrerender
prerenderUrlAsync(
    @NonNull WebView webView,
    @NonNull String url,
    @Nullable CancellationSignal cancellationSignal,
    @NonNull Executor callbackExecutor,
    @NonNull PrerenderOperationCallback callback
)

Starts a URL prerender request for this WebView.

static void
@RequiresFeature(name = WebViewFeature.PRERENDER_WITH_URL, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@WebViewCompat.ExperimentalUrlPrerender
prerenderUrlAsync(
    @NonNull WebView webView,
    @NonNull String url,
    @Nullable CancellationSignal cancellationSignal,
    @NonNull Executor callbackExecutor,
    @NonNull SpeculativeLoadingParameters params,
    @NonNull PrerenderOperationCallback callback
)

The same as prerenderUrlAsync, but allows customizing the request by providing SpeculativeLoadingParameters.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_LISTENER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
removeWebMessageListener(
    @NonNull WebView webview,
    @NonNull String jsObjectName
)

Removes the WebMessageListener associated with jsObjectName.

static void
@RequiresFeature(name = WebViewFeature.SAVE_STATE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@WebViewCompat.ExperimentalSaveState
saveState(
    @NonNull WebView webView,
    @NonNull Bundle outState,
    @IntRange(from = 1) int maxSizeBytes,
    boolean includeForwardState
)

Saves the state of the provided WebView, such as for use with onSaveInstanceState.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.MUTE_AUDIO, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAudioMuted(@NonNull WebView webView, boolean mute)

Mute or un-mute this WebView.

static void
@AnyThread
@RequiresFeature(name = WebViewFeature.DEFAULT_TRAFFICSTATS_TAGGING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setDefaultTrafficStatsTag(int tag)

Sets the default android.net.TrafficStats tag to use when accounting socket traffic caused by WebView.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setProfile(@NonNull WebView webView, @NonNull String profileName)

Sets the Profile with its name as the current Profile for this WebView.

static void
@AnyThread
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ALLOWLIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setSafeBrowsingAllowlist(
    @NonNull Set<String> hosts,
    @Nullable ValueCallback<Boolean> callback
)

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

static void
@AnyThread
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_WHITELIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setSafeBrowsingWhitelist(
    @NonNull List<String> hosts,
    @Nullable ValueCallback<Boolean> callback
)

This method is deprecated.

Please use setSafeBrowsingAllowlist instead.

static void
@RequiresFeature(name = WebViewFeature.CACHE_PROVIDER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@AnyThread
@WebViewCompat.ExperimentalCacheProvider
setShouldCacheProvider(boolean shouldCacheProvider)

Enables or disables caching of WebView provider objects (objects internal to the androidx.webkit library).

static void
@RequiresFeature(name = WebViewFeature.NAVIGATION_CALLBACK_BASIC, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@WebNavigationClient.ExperimentalNavigationCallback
setWebNavigationClient(
    @NonNull WebView webView,
    @NonNull WebNavigationClient client
)

Sets the WebNavigationClient for the given WebView.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setWebViewRenderProcessClient(
    @NonNull WebView webview,
    @Nullable WebViewRenderProcessClient webViewRenderProcessClient
)

Sets the renderer client object associated with this WebView.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setWebViewRenderProcessClient(
    @NonNull WebView webview,
    @NonNull Executor executor,
    @NonNull WebViewRenderProcessClient webViewRenderProcessClient
)

Sets the renderer client object associated with this WebView.

static void
@AnyThread
@RequiresFeature(name = WebViewFeature.START_SAFE_BROWSING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
startSafeBrowsing(
    @NonNull Context context,
    @Nullable ValueCallback<Boolean> callback
)

This method is deprecated.

In WebView version 122.0.6174.0 and later, this initialization is done automatically, so there is no need to call this API.

static void

Asynchronously trigger WebView startup.

Public methods

addDocumentStartJavaScript

Added in 1.9.0
@UiThread
@RequiresFeature(name = WebViewFeature.DOCUMENT_START_SCRIPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @NonNull <a href="/reference/an