如何在Android上使用ServiceWorkerController
在浏览Android
WebView文档时遇到了ServiceWorkerController,并决定尝试一下。不幸的是,我无法截获任何电话。我知道WebViewClient.shouldInterceptRequest,但有兴趣了解有关ServiceWorkerController的更多信息。不幸的是,这些文档比我下面已经实现的稀疏。任何帮助,将不胜感激。
我整理了一个由单个Activity组成的简单应用。
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();
}
}
}
和我有关 build.gradle
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'
}
回答:
我发现这个问题是个误会。我相信安装a
ServiceWorkerClient
可以让我拦截来自应用程序中所有WebView的所有请求,而不管其域如何。事实并非如此。该ServiceWorkerClient
只调用
如果 网页视图安装了ServiceWorker了。然后,来自所有服务人员的所有服务人员请求都将汇集到
ServiceWorkerClient
。这意味着,如果你想拦截所有的网页视图内的所有网络请求在您的应用程序,你也将需要重写shouldInterceptRequest()
您WebViewClient
为好。
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;
import androidx.annotation.Nullable;
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 swController = ServiceWorkerController.getInstance();
swController.setServiceWorkerClient(new ServiceWorkerClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
Log.e(LOGTAG, "in service worker. isMainFrame:"+request.isForMainFrame() +": " + request.getUrl());
return null;
}
});
swController.getServiceWorkerWebSettings().setAllowContentAccess(true);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
WebView.setWebContentsDebuggingEnabled(true);
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
Log.e(LOGTAG, "in webview client. isMainFrame:"+request.isForMainFrame() +": " + request.getUrl());
return super.shouldInterceptRequest(view, request);
}
});
Log.e(LOGTAG, "about to load URL");
//service worker handler only invoked on pages with service workers
mWebView.loadUrl("https://developers.google.com/web/fundamentals/primers/service-workers/");
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
以上是 如何在Android上使用ServiceWorkerController 的全部内容, 来源链接: utcz.com/qa/419330.html