I'm running my app that has webview on different activities, they are loading online pages, the issue is that everytime the activity opens the webview takes a couple of seconds to load the page and the page is not available offline.

我正在运行我的应用程序,其中包含不同活动的webview,他们正在加载在线页面,问题是每次活动打开时webview都需要几秒钟来加载页面,而且页面不能脱机使用。

I was looking for a solution where the webview would always be accessing an offline copy of the website, and the app would download and replace the local website every 24 hours.

我正在寻找一个解决方案,其中webview将始终访问该网站的离线副本,该应用程序将每24小时下载并替换本地网站。

I thought it would be simple Thanks

我觉得这很简单谢谢

1 个解决方案

#1


0

I see a couple potential solutions to this problem.

我看到了这个问题的几个潜在解决方案。

Change WebView Cache Mode

The first and simplest would be to change the cache mode of the WebView so that it is forced to load from the cache. This way it will try to load from the cache, and if any resources are not cached, then it will do a network load. This can be simply accomplished by using:

第一个也是最简单的方法是更改​​WebView的缓存模式,以便强制从缓存加载。这样它将尝试从缓存加载,如果没有缓存任何资源,那么它将进行网络加载。这可以通过以下方式简单地完成:

WebView webView; // initialized somewhere
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

Theoretically, you could use LOAD_CACHE_ONLY if the WebView has already loaded once and it doesn't need updating, otherwise you could use LOAD_DEFAULT. Using the LOAD_CACHE_ELSE_NETWORK should definitely speed up loading time, but it might still load some stuff from the network.

从理论上讲,如果WebView已经加载一次并且不需要更新,则可以使用LOAD_CACHE_ONLY,否则可以使用LOAD_DEFAULT。使用LOAD_CACHE_ELSE_NETWORK肯定会加快加载时间,但它仍然可能会从网络中加载一些东西。

However, in practice, I have seen the cache settings not work as consistently as I would like them to.

但是,在实践中,我已经看到缓存设置不能像我希望的那样一致地工作。

Utilize WebView.saveWebArchive()

Another more robust solution would be to use the web archive capability that the WebView has. I have not used this solution myself, but you can read about it in this question.

另一个更强大的解决方案是使用WebView具有的Web存档功能。我自己没有使用过这个解决方案,但你可以在这个问题中阅读它。

Basically, you can save the contents of the WebView using the following:

基本上,您可以使用以下方法保存WebView的内容:

WebView webView; // initialized somewhere
webView.loadUrl("http://blahblahblah.com"); // URL to save
Context context = this;
String pathFilename = context.getFilesDir().getPath() + File.separator + "savedWebPage.mht"; // You can use any path you want
webView.saveWebArchive(pathFilename);

Then you can load the saved archive file by using:

然后,您可以使用以下命令加载保存的存档文件:

webView.loadUrl("file://" + pathFilename);

Please note that I am leaving out the logic for handling when to load from the website and when to load from the archive file, that's up to you how to handle it.

请注意,我遗漏了处理何时从网站加载以及何时从归档文件加载的逻辑,这取决于您如何处理它。

更多相关文章

  1. android NDK 实用学习(四)-类缓存
  2. 必须在调用加载前设置广告尺寸和广告单元ID
  3. Android listview+SwipeRefreshLayout 下拉刷新,上拉加载.
  4. SmartRefreshLayout集成笔记,实现下拉刷新上拉加载更多。
  5. Android中如何有效的加载图片
  6. 无法使用@ContextConfiguration加载ApplicationContext(classes =
  7. 关于JAVA类加载大家发表一下见解吧
  8. Javascript实现页面加载完成后自动刷新一遍清除缓存文件
  9. Javascript 同步异步加载详解 (十足的好文章!!强烈推荐)

随机推荐

  1. Android中有时候运行程序的时候会报错:An
  2. 仿微信公众号界面实现
  3. . lang。例:Rect应该与child的界限相交。
  4. 安卓中无限轮播的实现
  5. 【Camera】Android平台Camera实时滤镜实
  6. Say Hello To Android(解析Android项目目
  7. 如何将音频文件与新视频文件合并?是否可以
  8. 在Skobbler中完成导航时,确定“街边”的最
  9. Fragment的setUserVisibleHint方法实现视
  10. Android L无法接听/拒接来电现象的分析与