WebView清除缓存

本文介绍如何在Android应用中清理系统缓存,并详细解释了WebView缓存机制及优化方法,包括视频播放配置和硬件加速技巧。

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

2.WebView缓存机制

在项目中经常会使用到WebView 控件,当加载html 页面时,会在/data/data/应用package 目录下生成database与cache 两个文件夹。请求的url 记录是保存在WebViewCache.db,而url 的内容是保存在WebViewCache 文件夹下

打开关闭使用缓存,一共有五个种类

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

//不使用缓存:
WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 
在退出应用的时候加上如下代码:

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

WebView缓存参考博客:

http://blog.youkuaiyun.com/a345017062/article/details/8703221

http://androiddada.iteye.com/blog/1280946

http://blog.youkuaiyun.com/a345017062/article/details/8573689

http://87426628.blog.163.com/blog/static/6069361820139183417725/


WebView播放视频:

1.参数设置:参数能加的都可以加上
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setAllowFileAccess(true);
mWebSettings.setDatabaseEnabled(true);
mWebSettings.setDomStorageEnabled(true);
mWebSettings.setSaveFormData(false);
mWebSettings.setAppCacheEnabled(true);
mWebSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
mWebSettings.setLoadWithOverviewMode(false);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setPluginState(PluginState.ON);
// WebView自适应屏幕大小
mWebSettings.setDefaultTextEncodingName("UTF-8");
mWebSettings.setLoadsImagesAutomatically(true);
// 设置可以支持缩放
mWebSettings.setSupportZoom(true);
// 设置默认缩放方式尺寸是far
mWebSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
// 设置出现缩放工具
mWebSettings.setBuiltInZoomControls(true);
这个参数一定不能设置: mWebSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);如果设置界面是空白
2.application添加android:hardwareAccelerated="true"
3.布局中设置 android:layerType="hardware"
<WebView
        android:id="@+id/cydsp_wv"
        android:layout_width="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_height="match_parent"
        android:layerType="hardware"
        android:layout_below="@+id/cydsp_title" />
如果设置这3步几乎不会出现不能播放问题





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值