有没有办法预先缓存网页以便使用Android WebView查看?

时间:2021-04-13 03:51:51

I've read about the HTML5 cache manifest, and I've seen Android does support caching websites using the cache manifest. I want to use the cache manifest to download all the required resources for my website to preload it, and then open a WebView and display the remote website using the pre-cached resources. I want to pre-cache my remote page somehow, preferably without using a WebView for the caching process.

我已经阅读了HTML5缓存清单,我看到Android确实支持使用缓存清单缓存网站。我想使用缓存清单为我的网站下载所有必需的资源以预加载它,然后打开WebView并使用预缓存的资源显示远程网站。我想以某种方式预先缓存我的远程页面,最好不使用WebView进行缓存过程。

The problem with using a WebView for the pre-caching process is that loading the webpage using a WebView renders and executes the page instead of just downloading it.

使用WebView进行预缓存过程的问题是,使用WebView加载网页会呈现并执行页面,而不仅仅是下载页面。

I've read this: http://alex.tapmania.org/2010/11/html5-cache-android-webview.html, but having a WebView support caching is not what I want. I want the loading process to be instantaneous (assuming the cache manifest / etags of the remote website are the same as the cached version) right after I finish pre-caching the resources, instead of waiting for the WebView to load up and cache everything on the first access.

我读过这个:http://alex.tapmania.org/2010/11/html5-cache-android-webview.html,但是拥有WebView支持缓存并不是我想要的。在完成预缓存资源之后,我希望加载过程是即时的(假设远程网站的缓存清单/ etags与缓存版本相同),而不是等待WebView加载并缓存所有内容第一次访问。

What is the correct way of pre-caching web pages for viewing later?

预先缓存网页以供以后查看的正确方法是什么?

Thanks!

1 个解决方案

#1


8  

What you want to do requires implementing a mechanism for app cache, linked resources, cookies, and local database store for HTML5 apps that use database API, and that's an important part of what browsers do in these days. I don't recommend doing the caching by yourself, not only because it's so much work, but also because I can't recall any method in WebView and it's friends (WebViewClient, etc.) that accepts an outside cache.

您想要做的是要求为使用数据库API的HTML5应用程序实现应用程序缓存,链接资源,cookie和本地数据库存储的机制,这是目前浏览器所做的重要部分。我不建议你自己进行缓存,不仅因为它的工作量很大,而且还因为我无法回想起WebView中的任何方法,而且它的朋友(WebViewClient等)接受外部缓存。

But your problem has a very simpler solution: you can put a WebView in your view and set its visibility to gone. Then make it visible when it has finished loading the page. WebView also automatically keeps the cache for your app so that the next time it runs it loads the page more quickly.

但是您的问题有一个非常简单的解决方案:您可以在视图中放置WebView并将其可见性设置为已消失。然后在加载页面后使其可见。 WebView还会自动保留应用程序的缓存,以便下次运行时可以更快地加载页面。

For hiding your WebView and then automatically showing it you just have to override onPageFinished in WebViewClient.

要隐藏WebView然后自动显示它,您只需在WebViewClient中覆盖onPageFinished。

#1


8  

What you want to do requires implementing a mechanism for app cache, linked resources, cookies, and local database store for HTML5 apps that use database API, and that's an important part of what browsers do in these days. I don't recommend doing the caching by yourself, not only because it's so much work, but also because I can't recall any method in WebView and it's friends (WebViewClient, etc.) that accepts an outside cache.

您想要做的是要求为使用数据库API的HTML5应用程序实现应用程序缓存,链接资源,cookie和本地数据库存储的机制,这是目前浏览器所做的重要部分。我不建议你自己进行缓存,不仅因为它的工作量很大,而且还因为我无法回想起WebView中的任何方法,而且它的朋友(WebViewClient等)接受外部缓存。

But your problem has a very simpler solution: you can put a WebView in your view and set its visibility to gone. Then make it visible when it has finished loading the page. WebView also automatically keeps the cache for your app so that the next time it runs it loads the page more quickly.

但是您的问题有一个非常简单的解决方案:您可以在视图中放置WebView并将其可见性设置为已消失。然后在加载页面后使其可见。 WebView还会自动保留应用程序的缓存,以便下次运行时可以更快地加载页面。

For hiding your WebView and then automatically showing it you just have to override onPageFinished in WebViewClient.

要隐藏WebView然后自动显示它,您只需在WebViewClient中覆盖onPageFinished。