当前位置:首页 > Android

Android WebView删除缓存

jsc10年前 (2016-09-19)Android4185

1. 删除保存于手机上的缓存

// 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;     
}


activity中的代码:

webView.clearCache(true);
clearCacheFolder(getApplicationContext().getCacheDir(),
System.currentTimeMillis());
getApplicationContext().deleteDatabase("webview.db");
getApplicationContext().deleteDatabase("webviewCache.db");
clearCacheFolder(getApplicationContext().getCacheDir(),
System.currentTimeMillis());
getApplicationContext().deleteDatabase("webview.db");
getApplicationContext().deleteDatabase("webviewCache.db");


2. 打开关闭使用缓存   

//优先使用缓存:
WebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);      
//不使用缓存:    
WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

3. 在退出应用的时候加上如下代码    

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");

扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/125.html

分享给朋友:

“Android WebView删除缓存” 的相关文章

ViewPager + HorizontalScrollView 实现可滚动的标签栏

ViewPager + HorizontalScrollView 实现可滚动的标签栏

这是一个可滑动的标签栏的自定义控件,参考此文章http://blog.csdn.net/fx_sky/article/details/8990573,我将主要的功能整合成一个类,配上2个特定的布局即可使用。 效果图:    主要布局文件:<?xml&nb…

GridView中item高度自适应

item高度自适应public class MyAdapter extends BaseAdapter {         GridView mGv;&n…

Sipnner点击相同Item不响应的解决方法

Spinner,两次点击同一个item的时候,第二次会不响应OnItemSelect事件。解决方法:@Override public void onItemSelected(AdapterView<?> arg0, View arg1,…

安卓对话框之----多选框(CheckBox)

安卓对话框之----多选框(CheckBox)

简单讲解一下原理:每个多选框都是独立的,可以通过迭代所有多选框,然后根据其状态是否被选中再获取其值。 CheckBox.setChecked(true);//设置成选中状态。 CheckBox.getText();//获取多选框的值 调用setOnCheckedChan…

Android经过Intent传输包含自定义类的ArrayList

Android通过Intent传输包含自定义类的ArrayList前言之前项目中通过Intent只是传输简单的字符串,这次因为需要在前一个页面联网获取对象数据,然后在下一个页面使用,所以考虑到使用Intent传输包含自定义类的ArrayList。SerializableJava的对象序列化指的是将那…

Activity的四种launchMode

Activity的四种launchMode

launchMode在多个Activity跳转的过程中扮演着重要的角色,它可以决定是否生成新的Activity实例,是否重用已存在的 Activity实例,是否和其他Activity实例公用一个task里。这里简单介绍一下task的概念,task是一个具有栈结构的对象,一个 task可以管理多个…