本文介绍了具有Service Worker的WebView(什么是ServiceWorkerController和ServiceWorkerWebSettings?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务人员在Chrome台式机和Chrome移动版上运行良好,我可以通过Chrome台式机中出色的DevTools(通过USB远程监控Chrome移动版)确切地了解正在发生的情况.我的服务人员非常紧密地基于此示例.该页面可以在没有网络连接的情况下重新加载正常,并且可以从DevTools的网络"选项卡中确认资源是否正在按预期方式由服务工作者进行缓存和提供服务.

I have a service worker going fine in Chrome desktop and Chrome mobile, and I can tell exactly what is happening via the amazing DevTools in Chrome desktop (monitoring Chrome mobile remotely via USB). My service worker is based very closely on this example. The page reloads fine without a network connection, and from the Network tab of DevTools I can confirm that resources are being cached and served by the service worker as expected.

但是我也正在WebView中加载同一页面,并且我正在努力确定服务工作者是否实际上在正确注册并按应有的方式工作.在Chrome台式机/移动设备中,我看不到使用DevTools进行调试的方法.

But I am also loading the same page in a WebView and I'm struggling to determine whether the service worker is actually registering properly and operating as it should. I can't see a way of debugging this in the same way as you can with DevTools in Chrome desktop/mobile.

可以确定的是,页面在WebView中时,如果没有连接就不会重新加载页面,因此不会缓存某些资源.尽管事实上服务工作者似乎在WebView中受支持.

What is certain is that the page doesn't reload without a connection when it is in the WebView, so some resources aren't being cached. This is despite the fact that service workers seem to be supported in WebView.

到处寻找可能的步骤或设置,我可能会找不到它来像在Chrome中那样工作,我遇到了 ServiceWorkerController ServiceWorkerWebSettings ,但找不到这些文档的任何文档或示例(链接的基本文档除外).在StackOverflow上搜索这些术语会得出零结果.

Looking around the place for possible steps or settings I might be missing to get it working as it does in Chrome, I came across ServiceWorkerController and ServiceWorkerWebSettings, but cannot find any documentation or examples for these (other than the basic docs linked). A search on StackOverflow for these terms gives zero results.

在WebView的某个地方是否存在一个工作示例,该示例可以加载带有附加服务人员的页面?任何想法如何使用以上两个类...它们实际上是必需的吗?

Is there a working example somewhere of a WebView that loads a page with attached service workers? Any idea how to use the above two classes... are they actually necessary?

推荐答案

这只是部分答案,但还是希望对您有所帮助:

This is only a partial answer, but hope it helps some anyways:

据我所知,ServiceWorkerController是ServiceWorker for WebView的Java实现,它与Javascript实现是分开的. 两者似乎没有以任何方式捆绑在一起.

From what I can tell, the ServiceWorkerController is the Java implementation of ServiceWorker for WebView, which is separate from the Javascript implementation. The two do not seem to be tied together in any way.

Java实现是一个单例,它会持久存在并影响应用程序中的所有WebView,它的唯一方法是通过shouldInterceptRequest拦截请求.它与WebViewClient非常相似,后者具有更多功能,并且仅会影响通过setWebViewClient附加到的WebView.

The Java implementation is a singleton which persists and affects all WebViews in an application, and its only method is to intercept requests via shouldInterceptRequest. It is very similar to WebViewClient, the latter of which has more functionality and only affects the WebViews it is attached to (via setWebViewClient).

您发布的链接似乎表明WebView确实支持ServiceWorker的Javascript版本.您是否偶然错过了通过WebSettings.setJavascriptEnabled启用Javascript?另外,请确保您的目标是最低SDK版本21,因为ServiceWorkers仅在Android 5.0及更高版本中启用.我很想知道您或任何人是否都设法使Javascript ServiceWorker也能在WebView中运行.

The link you posted does seem to indicate that WebViews do support the Javascript version of ServiceWorker. Did you by any chance miss enabling Javascript through WebSettings.setJavascriptEnabled? Also, ensure that you are targeting minimum SDK version 21 as ServiceWorkers are only enabled in Android 5.0 and higher. I am interested to know if you or anyone has managed to get a Javascript ServiceWorker functioning inside a WebView as well.

这篇关于具有Service Worker的WebView(什么是ServiceWorkerController和ServiceWorkerWebSettings?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 13:25