我正在处理应用程序并将数据保存在 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/