0

I want to put in my application the ability to clear the cache directly from the app itself, without having to go into the settings. Looking for here on Stack Overflow I found this snippet, but it has many errors. Can you help me to fix?

Preference cache;
cache = (Preference) this.findPreference("cache");
cache.setOnPreferenceClickListener(new OnPreferenceClickListener() {
     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();

}

eclipse marks mistake here File appDir = new File(cache.getParent());, here }, here public static boolean deleteDir(File dir),and here if (dir != null && dir.isDirectory()) {

  • you mean to say u want to clear database and shared pref right? Settings->manage application->ur application->clear data u want to do this programtically m i right? – KOTIOS Jul 13 '13 at 05:48
  • I just want to clear the cache without going into settings – Gianni rossi Jul 13 '13 at 05:51
  • possible duplicate of [Clear Cache App: Why it doesn't work?](http://stackoverflow.com/questions/17616966/clear-cache-app-why-it-doesnt-work). No need to post a duplicate of your own question. Add relevant details to the original question! – Siddharth Lele Jul 13 '13 at 05:52
  • I'm sorry, I added all possible details, but I have not yet received responses – Gianni rossi Jul 13 '13 at 05:56
  • why did you make the same post ? anyway, i've added now my answer on your original post. please delete this post as it's the exact same as before. – android developer Jul 13 '13 at 11:40

0 Answers0