Directly run the Code:
- /** File name: DataCleanManager. java * Description: main functions include clearing the internal/external cache, clearing the database, clearing sharedPreference, clearing files, and clearing the custom directory */
- Package com. test. DataClean;
- Import java. io. File;
- Import android. content. Context;
- Import android. OS. Environment;
- /*** Application data clearing manager */
- Public class DataCleanManager {
- /*** Clear the internal cache of the application (/data/com. xxx. xxx/cache) *** @ param context */
- Public static void cleanInternalCache (Context context ){
- DeleteFilesByDirectory (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 application SharedPreference (/data/com. xxx. xxx/shared_prefs) *** @ param context */
- Public static void cleanSharedPreference (Context context ){
- DeleteFilesByDirectory (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 the content in/data/com. xxx. xxx/files ** @ param context */
- Public static void cleanFiles (Context context ){
- DeleteFilesByDirectory (context. getFilesDir ());
- }
- /*** 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 (context );
- For (String filePath: filepath ){
- CleanCustomCache (filePath );
- }
- }
- /*** Delete method: Only files in a folder are deleted. 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 ();
- }
- }
- }
- }