android development tutorial on clearing android data cache example of clearing local data cache

  • 2020-05-27 07:07:34
  • OfStack


/*  *  wen   a   The name :  DataCleanManager.java  *  tracing      above :   The main function is to clear inside / External cache, clear database, clear sharedPreference To remove files And clear custom directories   */
import java.io.File;
import android.content.Context;
import android.os.Environment;
/** *  This application data clear manager  */
public class DataCleanManager {
    /** *  Clear the application internal cache (/data/data/com.xxx.xxx/cache) * * @param context */
    public static void cleanInternalCache(Context context) {
        deleteFilesByDirectory(context.getCacheDir());
    }
    /** *  Clear all databases for this application (/data/data/com.xxx.xxx/databases) * * @param context */
    public static void cleanDatabases(Context context) {
        deleteFilesByDirectory(new File("/data/data/"
                + context.getPackageName() + "/databases"));
    }
    /**
     * *  Clear this application SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * @param
     * context
     */
    public static void cleanSharedPreference(Context context) {
        deleteFilesByDirectory(new File("/data/data/"
                + context.getPackageName() + "/shared_prefs"));
    }
    /** *  Clear this application database by name  * * @param context * @param dbName */
    public static void cleanDatabaseByName(Context context, String dbName) {
        context.deleteDatabase(dbName);
    }
    /** *  remove /data/data/com.xxx.xxx/files The content of the  * * @param context */
    public static void cleanFiles(Context context) {
        deleteFilesByDirectory(context.getFilesDir());
    }
    /**
     * *  Removal of the external cache The content of the (/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param
     * context
     */
    public static void cleanExternalCache(Context context) {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            deleteFilesByDirectory(context.getExternalCacheDir());
        }
    }
    /** *  Clear the file under the custom path, use care, please do not delete by mistake. And only files in the directory can be deleted  * * @param filePath */
    public static void cleanCustomCache(String filePath) {
        deleteFilesByDirectory(new File(filePath));
    }
    /** *  Clear all data for this application  * * @param context * @param filepath */
    public static void cleanApplicationData(Context context, String... filepath) {
        cleanInternalCache(context);
        cleanExternalCache(context);
        cleanDatabases(context);
        cleanSharedPreference(context);
        cleanFiles(context);
        for (String filePath : filepath) {
            cleanCustomCache(filePath);
        }
    }
    /** *  Delete methods   Only files in a folder will be deleted if passed in directory It is a file and will not be processed  * * @param directory */
    private static void deleteFilesByDirectory(File directory) {
        if (directory != null && directory.exists() && directory.isDirectory()) {
            for (File item : directory.listFiles()) {
                item.delete();
            }
        }
    }
}


Related articles: