简体   繁体   English

如何清除缓存Android

[英]How to clear cache Android

I need to find a way how to clear the data which my application stores in cache.Basically I am using Fedor's ( Lazy load of images in ListView ) lazy list implementation and I want to clear the cache automatically when I have for example 100 images loaded.Any ideas how to do that?我需要找到一种方法来清除我的应用程序存储在缓存中的数据。基本上我正在使用 Fedor( 在 ListView 中延迟加载图像)延迟列表实现,并且我想在加载了例如 100 个图像时自动清除缓存.任何想法如何做到这一点?

EDIT: Code :编辑:代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    list=(ListView)findViewById(R.id.list);
    adapter=new LazyAdapter(this, mStrings);
    list.setAdapter(adapter);

    deleteCache(this);
    adapter.notifyDataSetChanged();


}

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

this will delete cache这将删除缓存

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

I hope this helps you in getting further我希望这能帮助你走得更远

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

    }

} 

you can call system service clearApplicationUserData() ( working only for >= KitKat version )您可以调用系统服务clearApplicationUserData() (仅适用于 >= KitKat 版本)

so you can do a check for version and then everything will go fine :) here is the code :所以你可以检查版本,然后一切都会好起来的:) 这里是代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ((ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE))
                    .clearApplicationUserData();
    }

If using Apache Commons IO, you can accomplish this with a single line (excluding try-catch):如果使用 Apache Commons IO,您可以使用一行代码(不包括 try-catch)完成此操作:

try {
    FileUtils.deleteDirectory(context.getCacheDir());
} catch (IOException e) {
    // Do some error handling
}

With Kotlin you can just call使用 Kotlin,您只需调用

File(context.cacheDir.path).deleteRecursively()

Same result as the other answers, but no need for the checks and looping through manually结果与其他答案相同,但无需手动检查和循环

Discard old adapter and attach the listview to a new instance of the adapter (listview will lose scroll position, though).丢弃旧适配器并将列表视图附加到适配器的新实例(尽管列表视图将丢失滚动位置)。 Example:例子:

    @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    m_adapter = new MyAdapter(this);
        final ListView listView = (ListView) getView()
                .findViewById(R.id.my_list);
        listView.setAdapter(m_adapter);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM