android - 如何只清除应用程序的缓存而不清除其他数据(共享偏好)

标签 android

我正在处理应用程序并将数据保存在 sharedpreference 和缓存中,我只想清除应用程序的缓存而不是 sharedpreference 数据。我该怎么做?这是我的代码

public void clearApplicationData(Context context) 
{
    File cache = context.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++) {
               System.out.println("Directory not null");
              boolean success = deleteDir(new File(dir, children[i]));
              if (!success) {
                 return false;
              }
           }
        }

        // The directory is now empty so delete it
        return dir.delete();
     }

最佳答案

when i run application it's clear all the data of local memory

那是因为这一行:

File appDir = new File(cache.getParent());

摆脱它。删除所有对 appDir 的引用。

如果你想删除getCacheDir(),那么删除getCacheDir(),而不是其他的东西,比如getParent( )getCacheDir()

关于android - 如何只清除应用程序的缓存而不清除其他数据(共享偏好),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32226167/

相关文章:

android - 适用于 Android 项目的类似 iOS 的 Storyboard工具?

android - 打开 XML 文件时看不到图形布局

android - 你如何在 Android 中执行退格转义字符?

android - Nexus 7 编译时没有出现按钮

android - Webdav 与 jackrabbit lib 在 android 上

android - 如何在Kotlin中设置从服务到 Activity 的回调?

android - 如何减少使用 Visual Studio 2015 构建的 apk 大小?

android - 如何在Eclipse中导入Android App项目源码?

android - 修复了 Android 上的元素出现在屏幕外的问题

android - 如何仅从一个 fragment 显示自定义操作栏 View