Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以编程方式清除Android应用程序中的缓存_Android - Fatal编程技术网

以编程方式清除Android应用程序中的缓存

以编程方式清除Android应用程序中的缓存,android,Android,以编程方式清除android应用程序中缓存的正确方法是什么。我已经在使用下面的代码,但它看起来不适合我 @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); clearApplicationData(); } public void clearApplicationData() { File cache = getCacheDir(

以编程方式清除android应用程序中缓存的正确方法是什么。我已经在使用下面的代码,但它看起来不适合我

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    clearApplicationData();
}

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("EEEEEERRRRRRROOOOOOORRRR", "**************** 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();
}
@覆盖
受保护的空onDestroy(){
//TODO自动生成的方法存根
super.ondestory();
clearApplicationData();
}
公共无效clearApplicationData(){
文件缓存=getCacheDir();
File appDir=新文件(cache.getParent());
if(appDir.exists()){
String[]children=appDir.list();
for(字符串s:子项){
如果(!s.equals(“lib”)){
deleteDir(新文件(appDir,s));
Log.i(“eeeeee rrrrrrr ooooo rrrrr”,“*******************文件/数据/应用程序包/”+s+“已删除**********************”;
}
}
}
}
公共静态布尔deleteDir(文件目录){
if(dir!=null&&dir.isDirectory()){
String[]children=dir.list();
for(int i=0;i

我认为应该将
clearApplicationData()
放在
super.OnDestroy()之前。


您的应用程序关闭后无法处理任何方法。

如果您正在查找自己应用程序的删除缓存,只需删除缓存目录,即可完成所有操作

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        deleteDir(dir);
    } 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();
    } else if(dir!= null && dir.isFile()) {
        return dir.delete();
    } else {
        return false;
    }
}
公共静态void deleteCache(上下文){
试一试{
File dir=context.getCacheDir();
deleteDir(dir);
}catch(异常e){e.printStackTrace();}
}
公共静态布尔deleteDir(文件目录){
if(dir!=null&&dir.isDirectory()){
String[]children=dir.list();
for(int i=0;i
来自dhams的答案是正确的(经过多次编辑后),但正如代码的多次编辑所显示的那样,很难编写正确而健壮的代码来删除目录(带有子目录)。因此,我强烈建议您使用API或其他API:

import org.apache.commons.io.FileUtils;

...

// Delete local cache dir (ignoring any errors):
FileUtils.deleteQuietly(context.getCacheDir());
PS:如果使用context.getExternalCacheDir()返回的目录,也要删除该目录

要能够使用Apache Commons IO,请将其添加到
依赖项部分的
build.gradle
文件中:

compile 'commons-io:commons-io:2.4'

我不确定,但我也播下了这段代码。 这种鳕鱼会更快,在我看来它也很简单。 只需获取应用缓存目录并删除目录中的所有文件

public boolean clearCache() {
    try {

        // create an array object of File type for referencing of cache files   
        File[] files = getBaseContext().getCacheDir().listFiles();

        // use a for etch loop to delete files one by one
        for (File file : files) {

             /* you can use just [ file.delete() ] function of class File
              * or use if for being sure if file deleted
              * here if file dose not delete returns false and condition will
              * will be true and it ends operation of function by return 
              * false then we will find that all files are not delete
              */
             if (!file.delete()) {
                 return false;         // not success
             }
        }

        // if for loop completes and process not ended it returns true   
        return true;      // success of deleting files

    } catch (Exception e) {}

    // try stops deleting cache files
    return false;       // not success 
}
它通过getBaseContext().getCacheDir().listFiles()获取文件数组中的所有缓存文件
然后在文件循环中逐个删除。delet()方法

context.cacheDir.deleteRecursively()
试试这个

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    clearApplicationData();
}

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("EEEEEERRRRRROOOOOOORRRR", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
            }
        }
    }
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        int i = 0;
        while (i < children.length) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
            i++;
        }
    }

    assert dir != null;
    return dir.delete();
}
@覆盖
受保护的空onDestroy(){
//TODO自动生成的方法存根
super.ondestory();
clearApplicationData();
}
公共无效clearApplicationData(){
文件缓存=getCacheDir();
File appDir=新文件(cache.getParent());
if(appDir.exists()){
String[]children=appDir.list();
for(字符串s:子项){
如果(!s.equals(“lib”)){
deleteDir(新文件(appDir,s));
Log.i(“eeeeee rrrrrr ooooooo rrrrr”,“*******************文件/数据/应用程序包/”+s+“已删除**********************”;
}
}
}
}
公共静态布尔deleteDir(文件目录){
if(dir!=null&&dir.isDirectory()){
String[]children=dir.list();
int i=0;
而(i
将此代码放入
onStop()
方法的
MainActivity

@Override
protected void onStop() {
    super.onStop();
    AppUtils.deleteCache(getApplicationContext());
}
公共类应用程序{
公共静态void deleteCache(上下文){
试一试{
File dir=context.getCacheDir();
deleteDir(dir);
}捕获(例外e){}
}
公共静态布尔deleteDir(文件目录){
if(dir!=null&&dir.isDirectory()){
String[]children=dir.list();
for(int i=0;i
此代码将删除应用程序的整个缓存,您可以检查应用程序设置,打开应用程序信息并检查缓存的大小。使用此代码后,缓存大小将为0KB。因此,它可以享受干净的缓存

 if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
            ((ActivityManager) context.getSystemService(ACTIVITY_SERVICE))
                    .clearApplicationUserData();
            return;
        }

if(Build.VERSION\u CODES.KITKAT如果您使用的是kotlin,那么:

context.cacheDir.deleteRecursively()

另外,请检查getExternalCacheDir()@以了解3ST我们是否可以仅为webview清除缓存?webview.clearCache(布尔包含的iskfiles)将清除应用程序中所有WebVIe的缓存可能是@RizwanAhmed的副本您是否找到了上述问题的解决方案?在您要删除应用程序缓存的操作中。可能是在OnDestroy或应用程序中的某个位置执行此操作。此代码从不删除文件。当dir.isDirectory()时==false它只是返回false。由于您无法删除非空目录,这将不起作用。您应该添加一个else:
如果(dir!=null&&dir.isDirectory()){…}
如果(dir.isFile()){return dir.delete();}
@dhams此方法是否也会清除我的共享首选项数据?因为我想
context.cacheDir.deleteRecursively()