如何清除应用程序的数据缓存至code [英] how to clear data cache of the application through code

查看:137
本文介绍了如何清除应用程序的数据缓存至code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想清楚了我的应用程序,增加了数据缓存的方案,现在我的设置 - >应用程序 - 清除>管理应用程序 - >我的应用程序 - >清除缓存。

i want to clear data cache programmatic of my application that is increasing, right now i am clearing from Settings->Applications->Manage Application->My Application->Clear Cache.

但我想通过编程来做到这一点,请大家帮帮我。

but i want to do it by programmatic, please help me.

推荐答案

在调用这个类,it`ll计算所有已安装的应用程序缓存文件,然后简单地从你的手机,这是不影响数据库或者个人将其删除数据。它会提高你的手机,并使其更快,缓存文件被删除

When you calling this class, it`ll calculate all installed application cache files and then simply delete it from your phone, which are not affected to database or your personal data. it will boost your phone and make it faster, cache file is removed

public class MyApplicationClass extends Application {

    private static MyApplicationClass instance;

    @Override
    public void onCreate() 
    {
        super.onCreate();
        instance = this;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    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();
    }
}

这篇关于如何清除应用程序的数据缓存至code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆