WebView的缓存处理(中)

来源:互联网 发布:模拟定位器软件下载 编辑:程序博客网 时间:2024/05/21 10:42

以下文章摘抄自网络,拼凑在一起了。

一、网页缓存
1、WebView默认的缓存路径

/data/data/package_name/cache/
/data/data/package_name/database/webview.db
/data/data/package_name/database/webviewCache.db

如上

 在手机本地 data/data/包名/cache/webviewCache 中放的是Webview显示过的URL的内容(例子中是图片)。我们可以把它导出,后缀改成对应图片的格式 打开看看~,而databases中的webviewCache.db 中放的就是图片地址和图片名字对应等信息 的表~ 导出后也可用SQLite Database Browser 等工具查看

webview.db中放的是url记录


2、缓存模式(5种)

LOAD_CACHE_ONLY:  不使用网络,只读取本地缓存数据

LOAD_DEFAULT:  根据cache-control决定是否从网络上取数据。

LOAD_CACHE_NORMAL: API level 17中已经废弃, 从API level 11开始作用同LOAD_DEFAULT模式

LOAD_NO_CACHE: 不使用缓存,只从网络获取数据.

LOAD_CACHE_ELSE_NETWORK,只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。
如:www.taobao.com的cache-control为no-cache,在模式LOAD_DEFAULT下,无论如何都会从网络上取数据,如果没有网络,就会出现错误页面;在LOAD_CACHE_ELSE_NETWORK模式下,无论是否有网络,只要本地有缓存,都使用缓存。本地没有缓存时才从网络上获取。
www.360.com.cn的cache-control为max-age=60,在两种模式下都使用本地缓存数据。

总结:根据以上两种模式,建议缓存策略为,判断是否有网络,有的话,使用LOAD_DEFAULT,无网络时,使用LOAD_CACHE_ELSE_NETWORK。

3、清除缓存

调用WebView的clearCache(boolean);
CacheManager.clear高版本中需要调用隐藏API。--此处不太理解啥意思

4、控制大小

可选方式:定时统计缓存大小、按时间顺序删除缓存。


二、H5缓存通过如下设置 

 如:mWebView.getSettings().setAppCacheEnabled(true);

1、缓存构成
根据setAppCachePath(String appCachePath)提供的路径,在H5使用缓存过程中生成的缓存文件。

2、缓存模式

无模式选择,通过setAppCacheEnabled(boolean flag)设置是否打开。默认关闭,即,H5的缓存无法使用。

3、清除缓存

找到调用setAppCachePath(String appCachePath)设置缓存的路径,把它下面的文件全部删除就OK了。

4、控制大小

通过setAppCacheMaxSize(long appCacheMaxSize)设置缓存最大容量,默认为Max Integer。
同时,可能通过覆盖WebChromeClient.onReachedMaxAppCacheSize(long requiredStorage, long quota, WebStorage.QuotaUpdater quotaUpdater)来设置缓存超过先前设置的最大容量时的策略。



下面文章转自:http://androiddada.iteye.com/blog/1280946

 Webview离线功能(优先cache缓存+cache缓存管理)!

1.优先缓存 

这里我们需要设置下:

WebSettings webSettings= webView.getSettings();

webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

//WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 默认不使用缓存!

LOAD_CACHE_ELSE_NETWORK的意思是:

Use cache if content is there, even if expired (eg, history nav) If it is not in the cache, load from network. Use withsetCacheMode(int).

如果内容已经存在cache 则使用cache,即使是过去的历史记录。如果cache中不存在,从网络中获取!

所以加上这句,不仅可以使用cache离线显示用户浏览过的内容,还可以在有网络的情况下优先调用缓存,为用户减少流量!~

我有些费解的是,这个设置在我看来是很有利于用户体验的 为什么google不把它设置成默认的呢? 还需要开发者手动打开。猜想可能是因为相同页面可能会更新的原因!

如果离线加载出现乱码 可参考: http://androidturing.iteye.com/blog/1280656

http://androiddada.iteye.com/

2.缓存管理:

(1)clearCacheFolder(Activity.getCacheDir(), System.currentTimeMillis());//删除此时之前的缓存.

 

Java代码  收藏代码
  1. // clear the cache before time numDays    
  2. private int clearCacheFolder(File dir, long numDays) {     
  3. int deletedFiles = 0;     
  4. if (dir!= null && dir.isDirectory()) {     
  5. try {     
  6. for (File child:dir.listFiles()) {     
  7. if (child.isDirectory()) {     
  8. deletedFiles += clearCacheFolder(child, numDays);    
  9. }    
  10.     
  11. if (child.lastModified() < numDays) {     
  12. if (child.delete()) {     
  13. deletedFiles++;    
  14. }    
  15. }    
  16. }    
  17. catch(Exception e) {     
  18. e.printStackTrace();    
  19. }    
  20. }    
  21. return deletedFiles;     
  22. }    

 

(2) 退出应用前删除缓存的方法!

Java代码  收藏代码
  1. File file = CacheManager.getCacheFileBaseDir();    
  2.   if (file != null && file.exists() && file.isDirectory()) { for (File item : file.listFiles()) { item.delete();    
  3.   }    
  4.   file.delete();    
  5.   }    
  6.   context.deleteDatabase("webview.db");    
  7.   context.deleteDatabase("webviewCache.db");    
  • 大小: 10.6 KB
0 0
原创粉丝点击