Android clears various data in the app. android clears the app
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 content under/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 ());
}
}
/**
** Be careful when clearing files in a custom path. Do not delete them 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 );
If (filepath = null ){
Return;
}
For (String filePath: filepath ){
CleanCustomCache (filePath );
}
}
/**
** Deletion method: Only files in a folder are deleted. If the input 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 ();
}
}
}
// Obtain the object
// Context. getExternalFilesDir () --> SDCard/Android/data/the package name/files/directory of your application. Generally, some data is stored for a long time.
// Context. getExternalCacheDir () --> SDCard/Android/data/your application package name/cache/directory, which generally stores temporary cache data
Public static long getFolderSize (File file) throws Exception {
Long size = 0;
Try {
File [] fileList = file. listFiles ();
For (int I = 0; I <fileList. length; I ++ ){
// If there are files below
If (fileList [I]. isDirectory ()){
Size = size + getFolderSize (fileList [I]);
} Else {
Size = size + fileList [I]. length ();
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return size;
}
/**
* Delete files and directories under a specified directory
*
* @ Param deleteThisPath
* @ Param filepath
* @ Return
*/
Public static void deleteFolderFile (String filePath, boolean deleteThisPath ){
If (! TextUtils. isEmpty (filePath )){
Try {
File file = new File (filePath );
If (file. isDirectory () {// if there are files below
File files [] = file. listFiles ();
For (int I = 0; I <files. length; I ++ ){
DeleteFolderFile (files [I]. getAbsolutePath (), true );
}
}
If (deleteThisPath ){
If (! File. isDirectory () {// delete a file
File. delete ();
} Else {// directory
If (file. listFiles (). length = 0) {// The directory contains no files or directories. Delete
File. delete ();
}
}
}
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
/**
* Formatting Unit
*
* @ Param size
* @ Return
*/
Public static String getFormatSize (double size ){
Double kiloByte = size/1024;
If (kiloByte <1 ){
Return size + "Byte ";
}
Double megaByte = kilobytes/1024;
If (megaByte <1 ){
BigDecimal result1 = new BigDecimal (Double. toString (kiloByte ));
Return result1.setScale (1, BigDecimal. ROUND_HALF_UP)
. ToPlainString () + "KB ";
}
Double gigaByte = megaByte/1024;
If (gigaByte <1 ){
BigDecimal result2 = new BigDecimal (Double. toString (megaByte ));
Return result2.setScale (1, BigDecimal. ROUND_HALF_UP)
. ToPlainString () + "MB ";
}
Double teraBytes = gigaByte/1024;
If (teraBytes <1 ){
BigDecimal result3 = new BigDecimal (Double. toString (gigaByte ));
Return result3.setScale (1, BigDecimal. ROUND_HALF_UP)
. ToPlainString () + "GB ";
}
BigDecimal result4 = new BigDecimal (teraBytes );
Return result4.setScale (1, BigDecimal. ROUND_HALF_UP). toPlainString ()
+ "TB ";
}
Public static String getCacheSize (File file) throws Exception {
Return getFormatSize (getFolderSize (file ));
}