Android This app data Purge manager Datacleanmanager

Source: Internet
Author: User

1. Overall analysis

1.1. The source code is given first, you can copy it directly.

/*** This application Data Purge manager*/ Public classDatacleanmanager {/*** * Clear this app internal cache (/data/data/com.xxx.xxx/cache) * * * *@paramContext*/     Public Static voidCleaninternalcache (Context context) {Deletefilesbydirectory (Context.getcachedir ()); }    /*** * Clear all databases of the application (/data/data/com.xxx.xxx/databases) * * * *@paramContext*/     Public Static voidcleandatabases (Context context) {Deletefilesbydirectory (NewFile ("/data/data/" + context.getpackagename () + "/databases")); }    /*** * Clear this app sharedpreference (/data/data/com.xxx.xxx/shared_prefs) * * *@paramContext*/     Public Static voidcleansharedpreference (Context context) {Deletefilesbydirectory (NewFile ("/data/data/" + context.getpackagename () + "/shared_prefs")); }    /*** * Clear the application database by name * * *@paramContext *@paramDbName*/     Public Static voidCleandatabasebyname (Context context, String DbName) {context.deletedatabase (dbName); }    /*** * Clear the contents of/data/data/com.xxx.xxx/files * * * *@paramContext*/     Public Static voidCleanfiles (Context context) {Deletefilesbydirectory (Context.getfilesdir ()); }    /*** * Clear the contents of the external cache (/mnt/sdcard/android/data/com.xxx.xxx/cache) * *@paramContext*/     Public Static voidCleanexternalcache (Context context) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Deletefilesbyd        Irectory (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 * * * *@paramFilePath*/     Public Static voidCleancustomcache (String filePath) {deletefilesbydirectory (NewFile (FilePath)); }    /*** * Clear all data for this app * * *@paramContext *@paramfilepath*/     Public Static voidCleanapplicationdata (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 * * * *@paramDirectory*/    Private Static voiddeletefilesbydirectory (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 LongGetfoldersize (File file)throwsException {LongSize = 0; Try{file[] fileList=File.listfiles ();  for(inti = 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 (); }        returnsize; }    /*** Delete files and directories in the specified directory * *@paramDeletethispath *@paramFilePath *@return     */     Public Static voidDeletefolderfile (String FilePath,BooleanDeletethispath) {        if(!Textutils.isempty (FilePath)) {            Try{File File=NewFile (FilePath); if(File.isdirectory ()) {//If there is a file belowFile files[] =File.listfiles ();  for(inti = 0; i < files.length; i++) {deletefolderfile (Files[i].getabsolutepath (),true); }                }                if(Deletethispath) {if(!file.isdirectory ()) {//if it is a file, deleteFile.delete (); } Else{//Catalogue                        if(File.listfiles (). Length = = 0) {//there is no file or directory under the directory, deleteFile.delete (); }                    }                }            } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }    }    /*** Format Unit * *@paramSize *@return     */     Public StaticString Getformatsize (Doublesize) {        Doublekilobyte = size/1024; if(Kilobyte < 1) {            returnSize + "Byte"; }        Doublemegabyte = kilobyte/1024; if(Megabyte < 1) {BigDecimal result1=NewBigDecimal (double.tostring (kilobyte)); returnResult1.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "KB"; }        DoubleGigaByte = megabyte/1024; if(GigaByte < 1) {BigDecimal result2=NewBigDecimal (double.tostring (megabyte)); returnResult2.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "MB"; }        Doubleterabytes = gigabyte/1024; if(Terabytes < 1) {BigDecimal RESULT3=NewBigDecimal (double.tostring (gigaByte)); returnResult3.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "GB"; } BigDecimal RESULT4=NewBigDecimal (terabytes); returnResult4.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "TB"; }     Public StaticString getcachesize (file file)throwsException {returngetformatsize (getfoldersize (file)); }}
View Code

1.2. Main functions

Clear the inside/out cache, clear the database, clear sharepreference, purge files, delete files, format units, etc.

  

1.3. Method List

    • Cleaninternalcache (Context) ==> clear this app internal cache
    • Cleandatabases (Context) ==> Clear all databases for this application
    • Cleansharepreference (Context) ==> Clear this application sharepreference
    • Cleandatabasebyname (context,string) ==> clear the application database by name
    • Cleanfiles (context) ==> clearing the contents of files
    • Cleanexternalcache (context) ==> clear the contents of the external cache
    • Cleancustomcache (String) ==> clear files under Custom path
    • Cleanapplicationdata (Context context,string ... filepath) ==> Erase all data from this app
    • Deletefilesbydirectory (file) ==> Delete files under a folder
    • Getfoldersize (File) ==> Gets the size of the current folder, including folders in the folder.
    • Deletefolderfile (string,boolean) ==> Delete files and directories under the specified directory
    • Getformatsize (double) ==> formatted units
    • Getcachesize (file) ==> get the size of the cache file


2. Local analysis

2.1. Clear the internal cache for this app

  

Passing in a context, you can get the internal cache path, and then call the Delete method

  

2.2. Clear all databases for this application

  

The Delete method is then called.

The file path of the database is databases.

2.3. Clear the application Sharepreference

  

The Delete method is also called.

Here's a good deal. The database file path has shared_prefs.

2.4. Clear the application database by name

  

The method in the context is called, and the database can be purged directly.

2.5. Clear the file under files under this application

  

This first obtains the address of this application files through the context, and then calls the Delete method.

2.6. Clear the files under the external cache

  

This is the first to determine if there is an external cache, then get the external cache address through the context, and then call the Delete method.

2.7. Clear the files under the custom path

  

You know a file here beforehand, you can invoke the Delete method, but only the file deletion in the directory is supported.

2.8. Clear all data for this app

  

This is where all of the above methods are called.

2.9. Get the size of the folder (there may be subdirectories in it)

  

Iterate through each file to get the sum of size sizes.

2.10. Delete files and directories in the specified directory

  

Give a specified directory, and then that parameter is a bit redundant. You can delete files using File.delete ().

2.11. Format units

  

Given a size, it is then calculated as a reasonable unit.

2.12. Get the cache size

  

Get a file parameter, determine the size of the folder, and then format the unit to know the size of the cache.


3. Usage examples

3.1. For example, in a set of activities

To know the size of the cache.

  

A getcachesize function of Datacleanmanager was called to obtain an external cache address in advance through Fileutil.

3.2. Clear the cache after clicking

  

A specified directory is given here, and the path is not deleted.


Android This app data Purge manager Datacleanmanager

Related Article

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.