Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;

import android.content.Context;
import android.os.Handler;

public class Main {
    /** Default on-disk cache directory. */
    private static final String DEFAULT_CACHE_DIR = "volley";

    public static void clearAppCache(final Context context, final Handler handler) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                File cacheDir = new File(context.getCacheDir().getPath(), DEFAULT_CACHE_DIR);
                clearCacheFolder(cacheDir, System.currentTimeMillis());
                handler.sendEmptyMessage(1);
            }
        }).start();

    }

    private static int clearCacheFolder(File dir, long curTime) {
        int deletedFiles = 0;
        if (dir != null && dir.isDirectory()) {
            try {
                for (File child : dir.listFiles()) {
                    if (child.isDirectory()) {
                        deletedFiles += clearCacheFolder(child, curTime);
                    }
                    if (child.lastModified() < curTime) {
                        if (child.delete()) {
                            deletedFiles++;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return deletedFiles;
    }
}