【问题标题】:Cache and temp files/folders remove android缓存和临时文件/文件夹删除 android
【发布时间】:2022-02-19 01:04:00
【问题描述】:

你好朋友感谢之前的回复, 我在删除缓存和临时文件/文件夹时遇到问题, 我需要的是从我的一个应用程序中清理整个设备临时文件和缓存 但在这里我只能清理我的应用程序缓存,这是我的代码

 private void mAppMethod(List<App> mApps) {
    // TODO Auto-generated method stub

            // File f = g
    for (int i = 0; i < mApps.size(); i++) {
          File dir = new  File("/data/data/"+mApps.get(i).getPackageName().concat("/cache"));

          Log.e("dir "+dir, "is directory "+dir.isDirectory());
            int j =    clearCacheFolder(dir, 10);
            if (dir!= null && dir.isDirectory())

                Log.e("j", "rff "+dir.delete());
            System.out.println(j+" rff "+dir.delete());

    }  

和我的清除缓存方法如下

 static int clearCacheFolder(final File dir, final int numDays) {

          int deletedFiles = 0;
          if (dir!= null && dir.isDirectory()) {
            //  System.out.println("here"+dir.delete());
              Log.e("here", "here  "+dir.isDirectory());
              try {

                  Log.e("here1", "here1"+dir.listFiles());
                  for (File child:dir.listFiles()) {
                      Log.e("here11", "here11");
                      //first delete subdirectories recursively
                      if (child.isDirectory()) {
                          Log.e("here111", "here111");
                          deletedFiles += clearCacheFolder(child, numDays);
                          Log.e("here1111", "here1111");
                      }
                      Log.e("here11111", "here11111");
                      //then delete the files and subdirectories in this dir
                      //only empty directories can be deleted, so subdirs have been done first
                      if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
                           Log.e("here111111", "here111111");
                          if (child.delete()) {
                               Log.e("here1111111", "here1111111");
                              deletedFiles++;
                              Log.e("here11111111", "here11111111");
                          }
                      }
                  }
              }
              catch(Exception e) {
                  Log.e("TAG", String.format("Failed to clean the cache, error %s", e.getMessage()));
              }
          }
          return deletedFiles;
      }

请帮助我如何清除整个设备缓存,我在这里获取每个应用程序的缓存位置,即设备中所有应用程序的缓存目录,但是当我想删除它们时,它返回 false

请帮助任何帮助是可观的

我能够清除一个应用程序的缓存,该应用程序是我正在运行此代码但不适用于其他应用程序的应用程序

提前致谢

【问题讨论】:

    标签: android caching


    【解决方案1】:

    试试这个代码

    public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());
        if(appDir.exists()){
            String[] children = appDir.list();
            for(String s : children){
                if(!s.equals("lib")){
                    deleteDir(new File(appDir, s));
                    Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s +" DELETED *******************");
                }
            }
        }
    }
    
    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
    
        return dir.delete();
    }
    

    这将删除您的所有数据,并在应用程序中缓存文件。

    【讨论】:

    • 普通的SDK应用程序无权访问,更不用说修改其他应用程序的缓存,更无权破解您的文件。这可能在以 root 身份运行应用程序的 root 手机上实现,在这种情况下,您必须根据应用程序的包名称手动构建路径。 stackoverflow.com/questions/4605571/…
    • 如果有可能在有根手机中,那么 android play 商店如何拥有缓存清理器应用程序以及我如何安装并且这些应用程序运行良好,如果有什么请帮忙
    • 和@jithu 此代码将删除我们正在处理的同一应用程序的缓存和其他文件,但不适用于Android手机的整个安装和本机应用程序
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多