Android清算应用缓存也清除了提供商

时间:2016-08-01 14:07:31

标签: android android-contentprovider

我使用以下代码删除了我的应用缓存。

public void clearApplicationData() {

        File cacheDirectory = getCacheDir();
        File applicationDirectory = new File(cacheDirectory.getParent());
        if (applicationDirectory.exists()) {
            String[] fileNames = applicationDirectory.list();
            for (String fileName : fileNames) {
                if (!fileName.equals("lib")) {
                    deleteFile(new File(applicationDirectory, fileName));
                }
            }
        }
    }

    public static boolean deleteFile(File file) {
        boolean deletedAll = true;
        if (file != null) {
            if (file.isDirectory()) {
                String[] children = file.list();
                for (int i = 0; i < children.length; i++) {
                    deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
                }
            } else {
                deletedAll = file.delete();
            }
        }
        return deletedAll;
    }

删除代码后,意味着删除我在清单中声明的​​提供程序。有没有办法在不删除内容提供商的情况下清除缓存?

1 个答案:

答案 0 :(得分:1)

您可以通过不删除数据库文件夹来避免这种情况

if (!fileName.equals("lib")&&!fileName.equals("files")&&!fileName.equals("database")) {
                    deleteFile(new File(applicationDirectory, fileName));
                }