How Android cleans up the various data in this app

Source: Internet
Author: User

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 of the application (/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 the contents of 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, please 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);
if (filepath = = null) {
Return
}
for (String Filepath:filepath) {
Cleancustomcache (FilePath);
}
}

/**
* * Delete method will only delete the file under a folder, if the incoming directory is a file, will not do processing *
*
* @param directory
*/
private static void Deletefilesbydirectory (File directory) {
if (directory! = null && directory.exists () && directory.isdirectory ()) {
For (File item:directory.listFiles ()) {
Item.delete ();
}
}
}

Get file
Context.getexternalfilesdir ()-sdcard/android/data/your app's package name/files/directory, usually put some long-time saved data
Context.getexternalcachedir ()-sdcard/android/data/your app package name/cache/directory, typically storing 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 is a file 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 in the 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 ()) {//If it is a file, delete
File.delete ();
} else {//directory
if (File.listfiles (). Length = = 0) {//directory without files or directories, delete
File.delete ();
}
}
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

/**
* Formatted units
*
* @param size
* @return
*/
public static String getformatsize (double size) {
Double kilobyte = size/1024;
if (kilobyte < 1) {
Return size + "Byte";
}

Double megabyte = kilobyte/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));
}

How Android cleans up the various data in this app

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.