Android Clear Cache编程不工作

Android Clear Cache编程不工作,android,caching,Android,Caching,我正试图通过编程方式清除应用程序中的数据和缓存。请在下面找到相同的代码 public static void deleteCache(Context context) { try { File cache = context.getApplicationContext().getCacheDir(); File appDir = new File(cache.getParent()); Log.d(T

我正试图通过编程方式清除应用程序中的数据和缓存。请在下面找到相同的代码

   public static void deleteCache(Context context) {
        try {

            File cache = context.getApplicationContext().getCacheDir();
            File appDir = new File(cache.getParent());
            Log.d(TAG,appDir.getAbsolutePath());
            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 *******************");
                    }
                }
            }

            File cache2= context.getApplicationContext().getExternalCacheDir();
            appDir = new File(cache2.getParent());
            Log.d(TAG,appDir.getAbsolutePath());
            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 *******************");
                    }
                }
            }

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                File cache1 = context.getApplicationContext().getDataDir();
                 appDir = new File(cache1.getParent());
                Log.d(TAG,appDir.getAbsolutePath());

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

            }






        } catch (Exception e) { e.printStackTrace();}
    }

    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();
    }
公共静态void deleteCache(上下文){
试一试{
文件缓存=context.getApplicationContext().getCacheDir();
File appDir=新文件(cache.getParent());
Log.d(标记,appDir.getAbsolutePath());
if(appDir.exists()){
String[]children=appDir.list();
for(字符串s:子项){
如果(!s.equals(“lib”)){
deleteDir(新文件(appDir,s));Log.i(“TAG”,“*************************文件/data/data/APP_包/“+s+”已删除****************************”;
}
}
}
文件cache2=context.getApplicationContext().getExternalCacheDir();
appDir=新文件(cache2.getParent());
Log.d(标记,appDir.getAbsolutePath());
if(appDir.exists()){
String[]children=appDir.list();
for(字符串s:子项){
如果(!s.equals(“lib”)){
deleteDir(新文件(appDir,s));Log.i(“TAG”,“*************************文件/data/data/APP_包/“+s+”已删除****************************”;
}
}
}
if(android.os.Build.VERSION.SDK\u INT>=android.os.Build.VERSION\u code.N){
文件cache1=context.getApplicationContext().getDataDir();
appDir=新文件(cache1.getParent());
Log.d(标记,appDir.getAbsolutePath());
if(appDir.exists()){
String[]children=appDir.list();
for(字符串s:子项){
如果(!s.equals(“lib”)){
deleteDir(新文件(appDir,s));Log.i(“TAG”,“*************************文件/data/data/APP_包/“+s+”已删除****************************”;
}
}
}
}
}catch(异常e){e.printStackTrace();}
}
公共静态布尔deleteDir(文件目录)
{
if(dir!=null&&dir.isDirectory()){
String[]children=dir.list();
for(int i=0;i
我已经从getCacheDir、getExternalCacheDir和getDataDir中删除了所有文件。但运行此代码后,我仍然可以看到我的应用程序缓存大小仍然相同。我现在该怎么办

我正在检查缓存的PFB图像

手动清除缓存和存储的PFB屏幕截图:


很可能什么都没有。我不知道您是如何检查缓存大小的,但它可能包括您的应用程序无法直接管理的内容。在应用程序信息部分,我正在检查缓存和用户数据的大小,但未被清除。但是当我点击Clear cache&Clear storage(清除缓存和清除存储)时,它变为0 kb,因此当应用程序更新时,我需要清除缓存和数据,但数据不清晰。我正在Android 10 One Plus上检查这一点handset@CommonsWare请查看更新的问题让我知道我应该怎么做“但当我单击Clear cache&Clear storage时,它变为0 kb”--假设操作系统正在清除您在执行此操作时无法直接访问的内容。“让我知道我该做什么”——什么都不做。有26000多种Android设备型号。他们的设置应用程序版本显示什么取决于他们的设备制造商。你不能控制它。您可以自己清除
getCacheDir()
getExternalCacheDir()
的内容。除此之外,其余的由操作系统完成。