java - 从列表中清除应用程序缓存

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

                });

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

最佳答案

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

关于java - 从列表中清除应用程序缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19653109/

相关文章:

caching - Infinispan 与 memcached 满足高并发需求

css - WordPress 加载更改的 css 和旧的 css

android - 在 Android 导航组件中检索目的地的深层链接

android - Android Studio如何打包来自多个库项目的单个AAR?

java - 使用 IndexOf 获取带有空格和引号的字符串的子字符串

java - 通过 ANT 检索 SVN 修订版

java - 堆栈溢出错误 : null in Android Studio | possible fix?

c++ - 将 A[i][j] 更改为 A[j][i] 时,遍历矩阵的速度变慢

java - 如何将 Vaadin 7 服务器推送应用到导航 View

java - HTTP 状态 400 - 客户端发送的请求在语法上不正确