/** File name: DataCleanManager. java * Description: The main functions are to clear the internal/external cache, clear the database, clear sharedPreference, clear files, and clear the custom directory */package com. test. dataClean; import java. io. file; import android. content. context; import android. OS. environment;/***** the application data clearing manager */public class DataCleanManager {/***** clears the internal cache of the application (/data/com. xxx. xxx/cache) ** @ param context */public static void cleanInternalCache (Context context) {deleteFilesByDirecto Ry (context. getCacheDir ();}/*** clear all databases of the application (/data/com. xxx. xxx/databases) ** @ param context */public static void cleanDatabases (Context context) {deleteFilesByDirectory (new File ("/data/" + context. getPackageName () + "/databases");}/*** clear the SharedPreference (/data/com. xxx. xxx/shared_prefs) ** @ param context */public static void cleanSharedPreference (Context context) {delet EFilesByDirectory (new File ("/data/" + context. getPackageName () + "/shared_prefs "));} /*** clear the application database by name ** @ param context * @ param dbName */public static void cleanDatabaseByName (Context context, String dbName) {context. deleteDatabase (dbName);}/*** clear/data/com. xxx. content in xxx/files ** @ param context */public static void cleanFiles (Context context) {deleteFilesByDirectory (context. get FilesDir ();}/*** clear content in the external cache (/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 files in the custom path. Be careful when using the command. Do not delete the files by mistake. Only files in the directory can be deleted ** @ param filePath */public static void cleanCustomCache (String filePath) {deleteFilesByDirectory (new File (filePath ));} /*** clear all data of this application ** @ param context * @ param filepath */public static void cleanApplicationData (Context context, String... filepath) {cleanInternalCache (context); cleanExternalCache (context); cleanDatabases (context); cleanSharedPreference (context); cleanFiles (c Ontext); for (String filePath: filepath) {cleanCustomCache (filePath) ;}}/*** the deletion method only deletes files in a folder, if the imported directory is a File, no processing will be performed ** @ param directory */private static void deleteFilesByDirectory (File directory) {if (directory! = Null & directory. exists () & directory. isDirectory () {for (File item: directory. listFiles () {item. delete ();}}}}
From: http://blog.csdn.net/h3c4lenovo/article/details/8252588