/* * Pieces Name: datacleanmanager.java * description : main functions are to clear the internal/external cache, clear the database, clear the sharedpreference, clear files and clear the custom directory */ import java.io.file;import android.content.context;import android.os.environment; /** * This app data Cleanup manager */public class DataCleanManager { /** * Clear this app internal cache (/data/data/com.xxx.xxx/cache) * * @param context */ Public static void cleaninternalcache (Context context) { deletefilesbydirectory (Context.getcachedir ()); } /** * Clear all databases for this app (/data/data/com.xxx.xxx/databases) * * @param Context */ public static void cleandatabases (Context context) { deletefilesbydirectory (New file ("/data/data/" + context.getpackagename () + "/ Databases ")); } /** * * Clear this app 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 the application database by name * * @param context * @param dbnaMe */ public static void cleandatabasebyname (Context context, string dbname) { context.deletedatabase (dbName) ; } /** * clear the contents of/data/data/com.xxx.xxx/files * * @param context */ public static void Cleanfiles (Context context) { deletefilesbydirectory ( Context.getfilesdir ()); } /** * * clear content under 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 file under the custom path, use caution, do not delete it by mistake. And only supports file deletion in directory * * @param filePath */ public static Void cleancustomcache (String filepath) { Deletefilesbydirectory (New file (FilePath)); } /** * Clear all data for this app * * @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 method Only the files under a folder are deleted, if the incoming directory is a file, it 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 (); } } }}
for Performance testing for apps I usually use:www.ineice.com
Share: Android clears local data cache code