我需要删除我在列表视图中单击的应用程序的缓存,该应用程序显示手机中安装的所有应用程序。我找到了这段代码:
public void clearApplicationData() {
File cache = 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++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
但这一个删除了我使用的当前应用程序的缓存,而不是我选择的。我不知道我是否清楚.. 我想要的是创建一个对话框,如果您单击是按钮清除该应用程序的缓存.. 到目前为止,我写了这个:
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Uri packageUri = Uri.parse("package:" + " "+app.packageName);
File file =new File("com.dd.application");
long size=file.length()/1024;
File cache = 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));
Toast.makeText(MainActivity.this, "Cache deleted of" + " " +packageUri+ size, Toast.LENGTH_LONG).show();
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + packageUri + " DELETED *******************");
}
}
}
}
});
实际上,当我单击“是”时,它会显示正确的包名称但不会清除缓存.. 解决方案?