title: How to use ServiceWorkerController on Android
body: <p>I came across <a href="https://developer.android.com/reference/android/webkit/ServiceWorkerController" rel="nofollow noreferrer">ServiceWorkerController</a> while looking through the Android WebView documentation and decided to give it a try. Unfortunately I have been unable to get intercept any calls. I am aware of <a href="https://developer.android.com/reference/android/webkit/WebViewClient#shouldInterceptRequest(android.webkit.WebView,%2520java.lang.String)" rel="nofollow noreferrer">WebViewClient.shouldInterceptRequest</a> but am interested in learning more about the ServiceWorkerController. Unfortunately the documents are a little sparse beyond what I have already implemented below. Any help would be appreciated. </p>
  
  <p>I have put together a simple app consisting of a single Activity. </p>
  
  <pre><code>package com.example.myapplication;
  
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.util.Log;
  import android.webkit.ServiceWorkerClient;
  import android.webkit.ServiceWorkerController;
  import android.webkit.WebResourceRequest;
  import android.webkit.WebResourceResponse;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  
  public class MainActivity extends Activity {
      private final static String LOGTAG = MainActivity.class.getSimpleName();
  
      private WebView mWebView;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          ServiceWorkerController.getInstance().setServiceWorkerClient(new ServiceWorkerClient() {
              @Override
              public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
                  Log.e(LOGTAG, "in service worker");
                  return null;
              }
          });
  
          setContentView(R.layout.activity_main);
  
          mWebView = (WebView) findViewById(R.id.activity_main_webview);
  
          WebSettings webSettings = mWebView.getSettings();
          webSettings.setJavaScriptEnabled(true);
  
  
          mWebView.setWebViewClient(new WebViewClient());
          Log.e(LOGTAG, "about to load URL");
          mWebView.loadUrl("https://www.google.com");
      }
  
      @Override
      public void onBackPressed() {
          if(mWebView.canGoBack()) {
              mWebView.goBack();
          } else {
              super.onBackPressed();
          }
      }
  }
  </code></pre>
  
  <p>And my relevant <code>build.gradle</code></p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 29
      buildToolsVersion "29.0.2"
      defaultConfig {
          applicationId "com.example.myapplication"
          minSdkVersion 24
          targetSdkVersion 29
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
          }
      }
  }
  
  dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      implementation 'androidx.appcompat:appcompat:1.0.2'
      implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  
      testImplementation 'junit:junit:4.12'
      androidTestImplementation 'androidx.test.ext:junit:1.1.0'
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
  }
  
  </code></pre>