安卓学习之(清理缓存)

来源:互联网 发布:阿里小号 注册淘宝 编辑:程序博客网 时间:2024/05/18 02:13

我们做的项目里面需要开机自动清理缓存,这个开机自动运行,网上找的材料都可以使用,这里就不分析了。。。

但是清理缓存这一块,上网找的材料都不可以。。

然后自己一顿研究,完成了这个小小的功能。。

希望对大家有所帮助。。

package com.test.android;
//用于删除webview缓存里的文件 和 文件夹
import java.io.File;


public class DeleteFile {

public static DeleteFile mInstance = null;

public static DeleteFile getInstance(){
if (null == mInstance) 
{
mInstance = new DeleteFile();
}
return mInstance;
}

/**
* 删除指定文件夹下所有文件
* param path 文件夹绝对路径
*/ 
    
public static void delFolder(String folderPath) {
    try 
    {
       delAllFile(folderPath); //删除完里面所有内容
      /* String filePath = folderPath;
       filePath = filePath.toString();
       File myFilePath = new File(filePath);
       myFilePath.delete(); //删除空文件夹*/
    } catch (Exception e) 
    {
      e.printStackTrace(); 
    }
}

/**
* 删除指定文件夹下所有文件
* param path 文件夹绝对路径
*/
  
public static boolean delAllFile(String path) 
  {
      boolean flag = false;
      File file = new File(path);
      if (!file.exists())
      {
        return flag;
      }
      if (!file.isDirectory()) 
      {
        return flag;
      }
      String[] tempList = file.list();
      File temp = null;
      for (int i = 0; i < tempList.length; i++) 
      {
         if (path.endsWith(File.separator)) 
         {
            temp = new File(path + tempList[i]);
         } 
         else 
         {
             temp = new File(path + File.separator + tempList[i]);
         }
         if (temp.isFile()) 
         {
            temp.delete();
         }
         if (temp.isDirectory()) 
         {
            delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
            //delFolder(path + "/" + tempList[i]);//再删除空文件夹
            flag = true;
         }
      }
      return flag;
    }

}

  直接就可以使用。。。如果只删除文件内容,不删除文件。。就关闭相应代码。。

原创粉丝点击