当我们加载Html时候,会在我们data/应用package下生成database与cache两个文件夹:
我们请求的Url记录是保存在webviewCache.db里,而url的内容是保存在webviewCache文件夹下.
WebView中存在着两种缓存:网页数据缓存(存储打开过的页面及资源)、H5缓存(即AppCache)。

一、网页缓存

1、缓存构成
/data/data/package_name/cache/
/data/data/package_name/database/webview.db

/data/data/package_name/database/webviewCache.db

WebView缓存文件结构如下图所示

再看一下 webviewCache 数据库结构

综合可以得知 webview 会将我们浏览过的网页url已经网页文件(css、图片、js等)保存到数据库表中

缓存模式(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。

设置WebView 缓存模式

  1. private void initWebView() {
  2. mWebView.getSettings().setJavaScriptEnabled(true);
  3. mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
  4. mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);  //设置 缓存模式
  5. // 开启 DOM storage API 功能
  6. mWebView.getSettings().setDomStorageEnabled(true);
  7. //开启 database storage API 功能
  8. mWebView.getSettings().setDatabaseEnabled(true);
  9. String cacheDirPath = getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME;
  10. //      String cacheDirPath = getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;
  11. Log.i(TAG, "cacheDirPath="+cacheDirPath);
  12. //设置数据库缓存路径
  13. mWebView.getSettings().setDatabasePath(cacheDirPath);
  14. //设置  Application Caches 缓存目录
  15. mWebView.getSettings().setAppCachePath(cacheDirPath);
  16. //开启 Application Caches 功能
  17. mWebView.getSettings().setAppCacheEnabled(true);
  18. }

清除缓存

  1. /**
  2. * 清除WebView缓存
  3. */
  4. public void clearWebViewCache(){
  5. //清理Webview缓存数据库
  6. try {
  7. deleteDatabase("webview.db");
  8. deleteDatabase("webviewCache.db");
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. }
  12. //WebView 缓存文件
  13. File appCacheDir = new File(getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME);
  14. Log.e(TAG, "appCacheDir path="+appCacheDir.getAbsolutePath());
  15. File webviewCacheDir = new File(getCacheDir().getAbsolutePath()+"/webviewCache");
  16. Log.e(TAG, "webviewCacheDir path="+webviewCacheDir.getAbsolutePath());
  17. //删除webview 缓存目录
  18. if(webviewCacheDir.exists()){
  19. deleteFile(webviewCacheDir);
  20. }
  21. //删除webview 缓存 缓存目录
  22. if(appCacheDir.exists()){
  23. deleteFile(appCacheDir);
  24. }
  25. }

完整代码

  1. package com.example.webviewtest;
  2. import java.io.File;
  3. import android.app.Activity;
  4. import android.graphics.Bitmap;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.webkit.JsPromptResult;
  9. import android.webkit.JsResult;
  10. import android.webkit.WebChromeClient;
  11. import android.webkit.WebSettings;
  12. import android.webkit.WebSettings.RenderPriority;
  13. import android.webkit.WebView;
  14. import android.webkit.WebViewClient;
  15. import android.widget.RelativeLayout;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18. public class MainActivity extends Activity {
  19. private static final String TAG = MainActivity.class.getSimpleName();
  20. private static final String APP_CACAHE_DIRNAME = "/webcache";
  21. private TextView tv_topbar_title;
  22. private RelativeLayout rl_loading;
  23. private WebView mWebView;
  24. private String url;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. //url:http://m.dianhua.cn/detail/31ccb426119d3c9eaa794df686c58636121d38bc?apikey=jFaWGVHdFVhekZYWTBWV1ZHSkZOVlJWY&app=com.yulore.yellowsdk_ios&uid=355136051337627
  30. url = "http://m.dianhua.cn/detail/31ccb426119d3c9eaa794df686c58636121d38bc?apikey=jFaWGVHdFVhekZYWTBWV1ZHSkZOVlJWY&app=com.yulore.yellowsdk_ios&uid=355136051337627";
  31. findView();
  32. }
  33. private void findView() {
  34. tv_topbar_title = (TextView) findViewById(R.id.tv_topbar_title);
  35. rl_loading = (RelativeLayout) findViewById(R.id.rl_loading);
  36. mWebView = (WebView) findViewById(R.id.mWebView);
  37. initWebView();
  38. mWebView.setWebViewClient(new WebViewClient() {
  39. @Override
  40. public void onLoadResource(WebView view, String url) {
  41. Log.i(TAG, "onLoadResource url="+url);
  42. super.onLoadResource(view, url);
  43. }
  44. @Override
  45. public boolean shouldOverrideUrlLoading(WebView webview, String url) {
  46. Log.i(TAG, "intercept url="+url);
  47. webview.loadUrl(url);
  48. return true;
  49. }
  50. @Override
  51. public void onPageStarted(WebView view, String url, Bitmap favicon) {
  52. Log.e(TAG, "onPageStarted");
  53. rl_loading.setVisibility(View.VISIBLE); // 显示加载界面
  54. }
  55. @Override
  56. public void onPageFinished(WebView view, String url) {
  57. String title = view.getTitle();
  58. Log.e(TAG, "onPageFinished WebView title=" + title);
  59. tv_topbar_title.setText(title);
  60. tv_topbar_title.setVisibility(View.VISIBLE);
  61. rl_loading.setVisibility(View.GONE); // 隐藏加载界面
  62. }
  63. @Override
  64. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  65. rl_loading.setVisibility(View.GONE); // 隐藏加载界面
  66. Toast.makeText(getApplicationContext(), "",
  67. Toast.LENGTH_LONG).show();
  68. }
  69. });
  70. mWebView.setWebChromeClient(new WebChromeClient() {
  71. @Override
  72. public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
  73. Log.e(TAG, "onJsAlert " + message);
  74. Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
  75. result.confirm();
  76. return true;
  77. }
  78. @Override
  79. public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
  80. Log.e(TAG, "onJsConfirm " + message);
  81. return super.onJsConfirm(view, url, message, result);
  82. }
  83. @Override
  84. public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
  85. Log.e(TAG, "onJsPrompt " + url);
  86. return super.onJsPrompt(view, url, message, defaultValue, result);
  87. }
  88. });
  89. mWebView.loadUrl(url);
  90. }
  91. private void initWebView() {
  92. mWebView.getSettings().setJavaScriptEnabled(true);
  93. mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
  94. mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);  //设置 缓存模式
  95. // 开启 DOM storage API 功能
  96. mWebView.getSettings().setDomStorageEnabled(true);
  97. //开启 database storage API 功能
  98. mWebView.getSettings().setDatabaseEnabled(true);
  99. String cacheDirPath = getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME;
  100. //      String cacheDirPath = getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;
  101. Log.i(TAG, "cacheDirPath="+cacheDirPath);
  102. //设置数据库缓存路径
  103. mWebView.getSettings().setDatabasePath(cacheDirPath);
  104. //设置  Application Caches 缓存目录
  105. mWebView.getSettings().setAppCachePath(cacheDirPath);
  106. //开启 Application Caches 功能
  107. mWebView.getSettings().setAppCacheEnabled(true);
  108. }
  109. /**
  110. * 清除WebView缓存
  111. */
  112. public void clearWebViewCache(){
  113. //清理Webview缓存数据库
  114. try {
  115. deleteDatabase("webview.db");
  116. deleteDatabase("webviewCache.db");
  117. } catch (Exception e) {
  118. e.printStackTrace();
  119. }
  120. //WebView 缓存文件
  121. File appCacheDir = new File(getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME);
  122. Log.e(TAG, "appCacheDir path="+appCacheDir.getAbsolutePath());
  123. File webviewCacheDir = new File(getCacheDir().getAbsolutePath()+"/webviewCache");
  124. Log.e(TAG, "webviewCacheDir path="+webviewCacheDir.getAbsolutePath());
  125. //删除webview 缓存目录
  126. if(webviewCacheDir.exists()){
  127. deleteFile(webviewCacheDir);
  128. }
  129. //删除webview 缓存 缓存目录
  130. if(appCacheDir.exists()){
  131. deleteFile(appCacheDir);
  132. }
  133. }
  134. /**
  135. * 递归删除 文件/文件夹
  136. *
  137. * @param file
  138. */
  139. public void deleteFile(File file) {
  140. Log.i(TAG, "delete file path=" + file.getAbsolutePath());
  141. if (file.exists()) {
  142. if (file.isFile()) {
  143. file.delete();
  144. } else if (file.isDirectory()) {
  145. File files[] = file.listFiles();
  146. for (int i = 0; i < files.length; i++) {
  147. deleteFile(files[i]);
  148. }
  149. }
  150. file.delete();
  151. } else {
  152. Log.e(TAG, "delete file no exists " + file.getAbsolutePath());
  153. }
  154. }
  155. }

简洁版代码:

  1. System.out.println("getCacheDir: "+WebViewActivity.this.getCacheDir());
  2. System.out.println("PackageResourcePath(): "+WebViewActivity.this.getPackageCodePath());
  3. System.out.println("getCacheDir: "+WebViewActivity.this.getPackageResourcePath());
  4. System.out.println("FilesDir: "+WebViewActivity.this.getDatabasePath("webview.db").getPath());
  5. System.out.println("FilesDir: "+WebViewActivity.this.getFilesDir().getPath())
  1. 03-31 11:54:52.094: I/System.out(22224): getCacheDir: /data/data/com.liao.webview/cache
  2. 03-31 11:54:52.094: I/System.out(22224): PackageResourcePath(): /data/app/com.liao.webview-1.apk
  3. 03-31 11:54:52.115: I/System.out(22224): getCacheDir: /data/app/com.liao.webview-1.apk
  4. 03-31 11:54:52.115: I/System.out(22224): FilesDir: /data/data/com.liao.webview/databases/webview.db
  5. 03-31 11:54:52.154: I/System.out(22224): FilesDir: /data/data/com.liao.webview/files
  6. 03-31 11:54:52.265: I/ActivityManager(59): Displayed activity com.liao.webview/.WebViewActivity: 418 ms (total 418 ms)
  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. if (child.lastModified() < numDays) {
  11. if (child.delete()) {
  12. deletedFiles++;
  13. }
  14. }
  15. }
  16. } catch(Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. return deletedFiles;
  21. }
  1. //优先使用缓存:
  2. WebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  1. <p>//不使用缓存:
  2. WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); </p>
  1. 退出的时候加上下面代码
  1. File file = CacheManager.getCacheFileBaseDir();
  2. if (file != null && file.exists() && file.isDirectory()) {
  3. for (File item : file.listFiles()) {
  4. item.delete();
  5. }
  6. file.delete();
  7. }
  8. context.deleteDatabase("webview.db");
  9. context.deleteDatabase("webviewCache.db");

WebView Cache 缓存清除的更多相关文章

  1. 王立平--WebView的缓存机制

    WebView的缓存能够分为页面缓存和数据缓存. 1.   页面缓存是指载入一个网页时的html.JS.CSS等页面或者资源数据. 这些缓存资源是因为浏览器的行为而产生.开发人员仅仅能通过配置HTTP ...

  2. Android WebView的缓存方式分析

    WebView的缓存可以分为(1)页面缓存和(2)数据缓存. 页面缓存是指当WebView加载一个网页时的html.JS.CSS等页面或者资源数据.这些缓存资源是由于浏览器的行为而产生,开发者只能通过 ...

  3. ms sql server缓存清除与内存释放

    Sql Server系统内 存管理在没有配置内存最大值,很多时候我们会发现运行Sql Server的系统内存往往居高不下.这是由于他对于内存使用的策略是有多少闲置的内存就占用多少,直到内存使用虑达到系 ...

  4. SQL Server 缓存清除与内存释放

    Sql Server系统内存管理在没有配置内存最大值,很多时候我们会发现运行SqlServer的系统内存往往居高不下.这是由于他对于内存使用的策略是有多少闲置的内存就占用多少,直到内存使用虑达到系统峰 ...

  5. Nginx 负载均衡的Cache缓存批量清理的操作记录

    1)nginx.conf配置 [root@inner-lb01 ~]# cat /data/nginx/conf/nginx.conf user www; worker_processes 8; #e ...

  6. SpringBoot日记——Cache缓存篇

    通常我们访问数据的情况如下图,数据存缓存就取缓存,不存缓存就取数据库,这样可以提升效率,不用一直读取数据库的信息: 开始记录: 关于SpringBoot缓存的应用 1. 首先在pom.xml文件中添加 ...

  7. springboot(九) Cache缓存和Redis缓存

    1. Cache缓存 1.1 缓存的概念&缓存注解 Cache 缓存接口,定义缓存操作.实现有:RedisCache.EhCacheCache.ConcurrentMapCache等 Cach ...

  8. ajax的cache缓存的使用方法

    ajax中cache缓存的使用: 问题描述: 在IE.360浏览器上提交表单后,保存后的内容不回显(依然显示空或者之前的内容). 原因: 回显内容是使用ajax的get方式的请求查询数据,ajax的c ...

  9. ASP.NET Core中使用Cache缓存

    ASP.NET Core中使用Cache缓存 缓存介绍: 通过减少生成内容所需的工作,缓存可以显著提高应用的性能和可伸缩性. 缓存对不经常更改的数据效果最佳. 缓存生成的数据副本的返回速度可以比从原始 ...

随机推荐

  1. JS中关于clientWidth offsetWidth srollWidth等的含义

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  2. linux 备份日志文件

    seo说要备份文件,然后自己搞不定,每天一份文件.写了个shell,加了个crontab -e任务.每天执行一次. crontab: 59 23 * * * /root/sh/dumpApacheLo ...

  3. [python] 视频008

    悬挂else if(hi>2) if(hi>7) printf('aaa') else printf('b') c语言中else会与就近if匹配 三元操作符 small=x if x< ...

  4. gnuplot使用

    直接用yum安装gnuplot即可,例如 sudo sh -c "yum install gnuplot.x86_64 " 安装以后就可以使用了 编写gnuplot脚本 # grp ...

  5. codevs 1557 热浪

    传送门 题目描述 Description 德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品.Farmer John此时以先天 ...

  6. (bug更正)利用KVC和associative特性在NSObject中存储键值

    KVC 一直没仔细看过KVC的用法,想当然的认为可以在NSObject对象中存入任意键值对,结果使用时碰到问题了. 一个简单的位移动画: CAKeyframeAnimation *keyPosi=[C ...

  7. uitableview的空白处不能响应 touchesbegan 事件

    现在的uitableview 的上面  响应不了     touchesbegan 事件   可能算是苹果的一个bug吧,不知道以后会不会改变 今天试了好久  都不行  最后  写了个字类  继承自 ...

  8. iOS:等待控件

    定义: @interface ViewController () { UIActivityIndicatorView *testActivityIndicator; } 实例化,开始旋转: -(voi ...

  9. GO语言中的指针

    http://www.tizgrape.com/?p=100 Go语言中的指针语法和C++一脉相承,都是用*作为符号,虽然语法上接近,但是实际差异不小. Go使用var定义变量: var v6 *in ...

  10. 听同事讲 Bayesian statistics: Part 1 - Bayesian vs. Frequentist

    听同事讲 Bayesian statistics: Part 1 - Bayesian vs. Frequentist   摘要:某一天与同事下班一同做地铁,刚到地铁站,同事遇到一熟人正从地铁站出来. ...