gpt4 book ai didi

java - 如何在 Android 上使用 ServiceWorkerController

转载 作者:行者123 更新时间:2023-12-02 02:49:05 24 4
gpt4 key购买 nike

我遇到了 ServiceWorkerController在查看 Android WebView 文档并决定尝试一下时。不幸的是,我一直无法拦截任何电话。我知道 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'
}

最佳答案

我发现这个问题是一个误解。我曾相信安装一个 ServiceWorkerClient 将允许我拦截来 self 的应用程序中所有 WebView 的所有请求,而不管域。情况似乎并非如此。 ServiceWorkerClient 仅在 webview 已经安装了 ServiceWorker 时才被调用。来自所有服务工作人员的所有服务工作人员请求然后都汇集到 ServiceWorkerClient。这意味着如果您希望拦截您应用中所有 WebView 中的所有网络请求,您还需要在您的 WebViewClient 上重写 shouldInterceptRequest()

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();
}
}
}

关于java - 如何在 Android 上使用 ServiceWorkerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62354423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com