ServiceWorkerControllerCompat

public abstract class ServiceWorkerControllerCompat


Manages Service Workers used by WebView.

Example usage:

ServiceWorkerControllerCompat swController = ServiceWorkerControllerCompat.getInstance();
swController.setServiceWorkerClient(new ServiceWorkerClientCompat() {
  @Override
  public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
    // Capture request here and generate response or allow pass-through
    // by returning null.
    return null;
  }
});

Summary

Public methods

static @NonNull ServiceWorkerControllerCompat
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getInstance()

Returns the default ServiceWorkerController instance.

abstract @NonNull ServiceWorkerWebSettingsCompat

Gets the settings for all service workers.

abstract void

Sets the client to capture service worker related callbacks.

Public methods

getInstance

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @NonNull ServiceWorkerControllerCompat getInstance()

Returns the default ServiceWorkerController instance. At present there is only one ServiceWorkerController instance for all WebView instances, however this restriction may be relaxed in the future.

This method should only be called if isFeatureSupported returns true for SERVICE_WORKER_BASIC_USAGE.

Returns
@NonNull ServiceWorkerControllerCompat

the default ServiceWorkerController instance

getServiceWorkerWebSettings

Added in 1.1.0
public abstract @NonNull ServiceWorkerWebSettingsCompat getServiceWorkerWebSettings()

Gets the settings for all service workers.

setServiceWorkerClient

Added in 1.1.0
public abstract void setServiceWorkerClient(@Nullable ServiceWorkerClientCompat client)

Sets the client to capture service worker related callbacks. A ServiceWorkerClientCompat should be set before any service workers are active, e.g. a safe place is before any WebView instances are created or pages loaded.