java - 如何清除android中的缓存数据

标签 java android

我编写了以下代码。但是我收到错误,因为不幸的是,cleardata 已停止。请任何人指导我。

package com.hrupin.cleaner;

import java.io.File;
import android.app.Application;
import android.util.Log;

public class MyApplication extends Application {
private static MyApplication instance;

@Override
public void onCreate() {
    super.onCreate();
    instance = this;
}

public static MyApplication getInstance(){
    return instance;
}

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

}

最佳答案

您应该在后台运行此类任务,而不是 UI 线程。

 Handler handler=new Handler();
 handler.post(new Runnable() {
        @Override
        public void run() {
            //put code here
        }
    });

关于java - 如何清除android中的缓存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31607201/

相关文章:

java - 在 spring 中创建新线程时如何保持上下文?

java - 删除 javafx TextArea 中选定的文本时出现异常

android - Flutter 应用程序仅在通过 APK 文件安装时停止。如何解决?

java - 检查参数中的类是否等于(instanceOf)所需的类。尝试创建通用类

android - 在 Android 上运行自己的 TensorFlow 模型会出现 native 推理错误 : "Session was not created with a graph before Run()!"

java - 数组的随机组合 (Java)

java - 类型安全地创建要传递给 Comparator.comparing() 的 Function 实例

java - 使用 Spring Roo 的 Hibernate OrderColumn

android - 双变量格式

java - 在 Android 上的 dalvik 中动态类重新加载