当前位置:首页 > Android

Android WebView删除缓存

jsc10年前 (2016-09-19)Android4354

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删除缓存” 的相关文章

Android中AsyncTask的简单用法

Android中AsyncTask的简单用法

在开发Android移动客户端的时候往往要使用多线程来进行操作,我们通常会将耗时的操作放在单独的线程执行,避免其占用主线程而给用户带来不好 的用户体验。但是在子线程中无法去操作主线程(UI 线程),在子线程中操作UI线程会出现错误。因此android提供了一个类Handler来在子线程中来更新UI线…

Android中Parcelable接口用法

1. Parcelable接口Interface for classes whose instances can be written to and restored from a Parcel。 Classes implementing the Parcelabl…

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

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

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

Android中从SD卡中/拍照选择图片并进行剪裁的方法

Android中从SD卡中/拍照选择图片并进行剪裁的方法

效果图: ˂img src="http://photo.j…

Android中的消息通知(NotificationManager和Notification)

下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提 示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个快讯。已添加的Notification.Builder,使其更容易构建通知。 notifica…

onTextChanged参数解释及实现EditText字数监听

由于最近做项目要检测EditText中输入的字数长度,从而接触到了Android中EditText的监听接口,TextWatcher。它有三个成员方法,第一个after很简单,这个方法就是在EditText内容已经改变之后调用,重点看下面两个方法:beforeTextChanged(CharSequ…