如何使用 Android WebView/TWA 安装 PWA

发布于 2025-01-16 08:49:25 字数 1485 浏览 3 评论 0原文

场景

我有一个现有的本机 Android 应用程序,我想在其中添加一些新功能。这些功能已经作为 PWA 实现,因为一些用户需要直接从网络使用它们。

我希望能够使用 Android WebView 打开 PWA(就好像它是我的本机应用程序的一部分)并“本地”安装它。对于“本地”,我的意思是 PWA 不应该作为手机中的应用程序可见,它只是我现有的本机应用程序的一部分。在某种程度上,我想要实现的是“插件”系统之王,我可以在其中拥有用户可以安装到本机应用程序的额外功能。

我被困在哪里了?

使用 WebView 打开 PWA 相当简单。我无法做的部分是安装 PWA。也许环球航空可以解决这个问题,但我不确定它是为此目的。

此外,理想情况下,我希望将所有内容保留在 Fragment 中,因为我希望工具栏和其他内容由本机应用程序创建(这就是为什么我更倾向于使用 WebView 而不是 TWA)。

编辑----------------

经过一番折腾后,我了解到它实际上是与 WebView 一起安装的。由于我无法在网上找到任何有关它如何工作的信息,所以我一直在自己研究它。

首先,ServiceWorkerController 仅在 Android 7 (24) 中可用。我无法测试 Android 6 是否可以工作,因为我没有任何该版本的手机,并且由于某种原因我无法更新模拟器的 WebView apk。

然后,我发现网页在我的新模拟器中崩溃了,因为安装的 WebView apk 太低并且不支持一些新功能。因此,要考虑安装的 WebView 版本的另一件事。该 apk 与 Android 版本分开安装(就好像它是您手机上的应用程序一样)。解决方案是仅使用真实设备,因为 apk 通常始终是最新的。

如下图所示,PWA 安装在应用程序文件夹内,并且还将所有信息(数据库、本地存储、会话存储等)保存在其中。 输入图片这里的描述

目前还不清楚如何处理这些数据(我创建了这个问题),但至少在以下情况下似乎是这样:

  1. WebStorage.Instance.DeleteAllData()
  2. 应用程序已卸载
  3. 应用程序数据已清除

清除应用程序的缓存不会删除持久数据。

所以现在我想说的问题是:

  1. Service Worker 从哪个 Android 版本安装?就从支持Service Workers的webview apk版本的Android版本开始?
  2. 它完全依赖于 ServiceWorkerController 吗?或者它只是一个实用程序类,可以更好地控制所发生的事情?

Scenario

I have an existing native android app in which I want to add some new features. These features have already been implemented as a PWA because some users needed to use them directly from the web.

I want to be able to open the PWA with an Android WebView (as if it was part of my native application) and install it 'locally'. With 'locally' I mean that the PWA should not be visible as an app in the phone, it would just be part of my existing native app. In a way, what I'm trying to achieve is a king of "plugin" system, where I could have extra features that the user can install to the native app.

Where am I stuck?

Opening the PWA with a WebView is fairly straight forward. The part that I am unable to do is installing the PWA. Maybe TWA could solve this issue, but I am not sure it is meant for that.

Moreover, ideally I would like to keep everything in a Fragment since I would like to have the toolbar and others created by the native app (that is why I've leaned more towards using WebView instead of TWA).

EDIT---------------

After messing around I was able to understand that it actually gets installed with WebView. Since I haven't been able to find any information about how it works online I've been researching it on my own.

First of all ServiceWorkerController is only available from Android 7 (24). I haven't been able to test if with Android 6 this can work or not due to the fact that I don't have any phone with that version and I can't update the WebView apk of my emulators for some reason.

Then, I found out that the webpage crashed in my newer emulators because the WebView apk installed was too low and didn't support some newer features. So another thing to take into consideration the WebView version installed. This apk gets installed separately from the Android version (it's as if it was an app on your phone). The solution to this is to just use a real device, because the apk is usually always up to date.

As you can see in the following picture the PWA gets installed inside the app folder and it also keeps all the info (databases, localstorage, sessionstorage, etc.) in there.
enter image description here

It is unclear how this data is handled (I created this question) but it appears to be at least in the following cases:

  1. WebStorage.Instance.DeleteAllData()
  2. App uninstalled
  3. App Data Cleared

Clearing the Cache of the app doesn't delete the persistent data.

So now I would say that the question would be:

  1. From which Android Version does the Service Worker install? Just from the Android version that has support for the webview apk version that supports Service Workers?
  2. Does it depend at all on ServiceWorkerController? Or is it just a utility class to have more control over what happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文