从列表清除应用程序缓存

时间:2013-10-29 08:28:09

标签: java android caching android-listview android-alertdialog

我需要删除我在listview中单击的应用程序的缓存,其中显示了手机中安装的所有应用程序..我找到了这段代码:

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 *******************");
                                }
                            }
                        }
                    }

                });

实际上当我单击是时,它会显示正确的包名但不清除缓存..解决方案?

1 个答案:

答案 0 :(得分:1)

您无法清除除您自己以外的任何应用程序的应用程序缓存。这将是一个严重的安全漏洞。 getCacheDir()方法仅返回对应用程序缓存目录的引用。