chetan j
chetan j

Reputation: 1

how to clear cached data in android

I wrote following code.but I am getting error as unfortunately cleardata has stopped. Any one please guide me.

package com.hrupin.cleaner;

import java.io.File;
import android.app.Application;
import android.util.Log;

public class MyApplication extends Application {
private static MyApplication 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();
}

}

Upvotes: 0

Views: 77

Answers (1)

user3290180
user3290180

Reputation: 4410

You should run this kind of tasks in background, not UI thread.

 Handler handler=new Handler();
 handler.post(new Runnable() {
        @Override
        public void run() {
            //put code here
        }
    });

Upvotes: 1

Related Questions