Clear Application cache on exit in android

后端 未结 3 973
广开言路
广开言路 2020-11-30 04:16

What I want to do is to clear the cache memory of application on exit of application.

this task i can do manually by this steps.

< Apps --> Manage

3条回答
  •  隐瞒了意图╮
    2020-11-30 04:46

    To clear Application Data Please Try this way. I think it help you.

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

提交回复
热议问题