Android清除WebView的localStorage

关于webview的缓存,在不同设备上会有不同的目录。

APP的登录状态和H5的登录状态常常因为某些原因不能同步。

故想要每次关闭webview页面时,都可以清除h5缓存的数据。

通过分析,web页面常用的存储有:cookie、localStorage、session

最终实现用一下方法清除缓存(有时会失效,极小概率)

WebStorage.getInstance().deleteAllData();

当然,也尝试过其他方法

CookieSyncManager.createInstance(getApplicationContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.removeAllCookie();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.removeSessionCookies(null);
    cookieManager.removeAllCookie();
    cookieManager.flush();
} else {
    cookieManager.removeAllCookie();
    CookieSyncManager.getInstance().sync();
}
deleteFile(new File(getCacheDir().getParentFile().getAbsolutePath(),"app_webview"));



public void deleteFile(File file) {


        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                File files[] = file.listFiles();
                for (File value : files) {
                    deleteFile(value);
                }
            }
//            Timber.d("deleteFile   delete file path=" + file.getAbsolutePath()+","+file.delete());
            new SecurityManager().checkDelete(file.getAbsolutePath());
            Timber.d("deleteFile   delete file path=" + file.getAbsolutePath());
        } else {
            Timber.d("deleteFile   delete file no exists " + file.getAbsolutePath());
        }
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值