如何从android清除所有Android应用程序缓存数据

时间:2014-03-12 13:59:54

标签: android

我正在搜索代码以清除android mobile中的所有缓存数据。我使用PackageManager和Method api清除android中的缓存数据,但这将发生在单个应用程序缓存数据

但我想要清除所有android应用程序缓存数据。

1 个答案:

答案 0 :(得分:0)

尝试使用一个应用程序缓存

public static void deleteCache(Context context) {
        try {
            File dir = context.getCacheDir();
            if (dir != null && dir.isDirectory()) {
                deleteDir(dir);
            }
        } catch (Exception e) {}
    }

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

也试一试。

public static void trimCache(Context context) {
    File dir = context.getCacheDir();
    if(dir!= null && dir.isDirectory()){
        File[] children = dir.listFiles();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            File temp;
            for (int i = 0; i < children.length; i++) {
                temp = children[i];
                temp.delete();
            }
        }

    }

}