Android WebView清除缓存,总有一个方法适合你

一,缓存介绍

缓存分为:页面缓存和数据缓存

页面缓存: 指加载一个网页时的html、JS、CSS等页面或者资源数据。

数据缓存 : 数据缓存分为AppCache和DOM Storage两种。

一般清除指的是数据缓存;

注意:以下清理缓存的方法,没有区分是那种缓存;

二,各种清楚缓存的方法

1,清除数据库缓存

context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");

2,清楚历史

webView.clearHistory();

3,清空Cookie

关于这个有几种写法:

a

CookieSyncManager.createInstance(this);         
CookieManager cookieManager = CookieManager.getInstance();        
cookieManager.removeAllCookie();

b

CookieSyncManager.createInstance(this);         
CookieManager cookieManager = CookieManager.getInstance();        
cookieManager.removeAllCookies(ValueCallback);

c

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
    CookieManager.getInstance().removeAllCookies(null);
    CookieManager.getInstance().flush();
}

d

CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       cookieManager.removeSessionCookies(null);
       cookieManager.removeAllCookie();
       cookieManager.flush();
} else {
       cookieManager.removeSessionCookies(null);
       cookieManager.removeAllCookie();
       CookieSyncManager.getInstance().sync();
}

4,清空Localstorage

WebStorage.getInstance().deleteAllData(); //清空WebView的localStorage

5,其他方法:设置统一的缓存路径,然后需要清楚数据时候,遍历每个路径下的文件然后一一删除;

三,其他和缓存相关的API

webView.clearFormData();
//设置缓存模式
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//有五种缓存模式
//设置数据库缓存路径
webView.getSettings().setDatabasePath(cacheDirPath);
//设置应用缓存目录
webView.getSettings().setAppCachePath(cacheDirPath);
//DOM存储功能
webView.getSettings().setDomStorageEnabled(true);
//数据库存储功能
webView.getSettings().setDatabaseEnabled(true);
//应用缓存
webView.getSettings().setAppCacheEnabled(true);

四,其他

调用系统浏览器去下载文件(loadUrl是下载地址):

Intent intent= new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse(loadUrl);
intent.setData(content_url);  
startActivity(Intent.createChooser(intent, "请选择浏览器"));

 

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ang_qq_252390816

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值