android webview的缓存机制

在运行安卓程序时,报错DatabaseObjectNotClosedException

运行是/data/data/package/databases/webview.db没关掉db

大概是跟webview的缓存有关?cache还是cookie?


于是研究了一下webview的缓存


webview缓存策略:


//优先使用缓存:

WebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);  

 

//不使用缓存:

WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);



缓存策略为,判断是否有网络,有的话,使用LOAD_DEFAULT,无网络时,使用LOAD_CACHE_ELSE_NETWORK


让我们看一下如何将Cookie和WebView进行结合:

Cooki的获取:


1

2

CookieManager cm = CookieManager.getInstance();

String Cookiestr = cm.getCookie(url);


Cookie的同步:




CookieSyncManager.createInstance(this);

CookieSyncManager.getInstance().sync();


注意,此处代码需在在webview加载URL前设置,并且在调用后,不再设置webview的websetttings属性,否则会导致失效.


一般通过HttpClient,获取服务器的Cookie后再进行设置(比如一些登录的用户信息或者状态信息):



List<cookie> cookies = httpclient.getCookieStore().getCookies();  


3、清除缓存

clearCache(boolean)

CacheManager.clear高版本中需要调用隐藏API。


清除Cookie:


1

CookieManager.getInstance().removeSessionCookie();

1

CookieManager.getInstance().removeAllCookies();



如果只是需要删除和特定url关联的cookie,需要先进行移除后同步:

1

2

3

4

5

6

7

8

9

10

11

/**

 * 同步一下cookie

 */

public static void synCookies(Context context, String url) {

    CookieSyncManager.createInstance(context);

    CookieManager cookieManager = CookieManager.getInstance();

    cookieManager.setAcceptCookie(true);

    cookieManager.removeSessionCookie();//移除

    cookieManager.setCookie(url, cookies);//指定要修改的cookies

    CookieSyncManager.getInstance().sync();

}



二、H5缓存


1、缓存构成

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


2、缓存模式

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


3、清除缓存

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



4删除缓存

// clear the cache before time numDays    

private int clearCacheFolder(File dir, long numDays) {         

    int deletedFiles = 0;        

    if (dir!= null && dir.isDirectory()) {            

        try {               

            for (File child:dir.listFiles()) {   

                if (child.isDirectory()) {             

                    deletedFiles += clearCacheFolder(child, numDays);         

                }   

                if (child.lastModified() < numDays) {    

                    if (child.delete()) {                  

                        deletedFiles++;          

                    }   

                }   

            }            

        } catch(Exception e) {      

            e.printStackTrace();   

        }    

    }      

    return deletedFiles;    

}



6 退出应用的时候 :

File file = CacheManager.getCacheFileBaseDir();  

   if (file != null && file.exists() && file.isDirectory()) {  

    for (File item : file.listFiles()) {  

     item.delete();  

    }  

    file.delete();  

   }  

   

  context.deleteDatabase("webview.db");  

  context.deleteDatabase("webviewCache.db”);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值