* * File Name: Datacleanmanager.java * Description: The main function is to clear the internal/external cache, clear the database, clear the sharedpreference, clear the files and clear the custom directory * *
Import Java.io.File;
Import Android.content.Context;
Import android.os.Environment;
/** * The Application Data Removal Manager * *
public class Datacleanmanager {
/** * Clear this 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 the 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 the application database by name * * @param context * @param dbname * *
public static void Cleandatabasebyname (context context, String dbname) {
Context.deletedatabase (dbname);
}
/** * Clear the content under/data/data/com.xxx.xxx/files * * @param context * *
public static void Cleanfiles (context context) {
Deletefilesbydirectory (Context.getfilesdir ());
}
/**
* * Clear the external cache content (/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 custom path of the file, use caution, please do not accidentally deleted. And only file deletion in directory is supported * * @param filePath * *
public static void Cleancustomcache (String filePath) {
Deletefilesbydirectory (New File (FilePath));
}
/** * Erase all data in this application * * @param context * @param filepath */
public static void CL Eanapplicationdata (Context, String ... filepath) {
Cleaninternalcache (context);
Cleanexternalcache (context);
cleandatabases (context);
cleansharedpreference (context);
cleanfiles (context);
for (String filepath:filepath) {
Cleancustomcache (FilePath);
}
}
/** * Delete method only files under a folder are deleted here, and 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 ();
}
}
}
}