Case study of Android clearing local data cache code

Source: Internet
Author: User

Directly run the Code:

 
 
  1. /** 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 */
  2. Package com. test. DataClean;
  3. Import java. io. File;
  4. Import android. content. Context;
  5. Import android. OS. Environment;
  6. /*** Application data clearing manager */
  7. Public class DataCleanManager {
  8. /*** Clear the internal cache of the application (/data/com. xxx. xxx/cache) *** @ param context */
  9. Public static void cleanInternalCache (Context context ){
  10. DeleteFilesByDirectory (context. getCacheDir ());
  11. }
  12. /*** Clear all databases of the application (/data/com. xxx. xxx/databases) *** @ param context */
  13. Public static void cleanDatabases (Context context ){
  14. DeleteFilesByDirectory (new File ("/data/" + context. getPackageName () + "/databases "));
  15. }
  16. /*** Clear the application SharedPreference (/data/com. xxx. xxx/shared_prefs) *** @ param context */
  17. Public static void cleanSharedPreference (Context context ){
  18. DeleteFilesByDirectory (new File ("/data/" + context. getPackageName () + "/shared_prefs "));
  19. }
  20. /*** Clear the application database by name ** @ param context * @ param dbName */
  21. Public static void cleanDatabaseByName (Context context, String dbName) {context. deleteDatabase (dbName );
  22. }
  23. /*** Clear the content in/data/com. xxx. xxx/files ** @ param context */
  24. Public static void cleanFiles (Context context ){
  25. DeleteFilesByDirectory (context. getFilesDir ());
  26. }
  27. /*** Clear content in the external cache (/mnt/sdcard/android/data/com. xxx. xxx/cache) *** @ param context */
  28. Public static void cleanExternalCache (Context context ){
  29. If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED )){
  30. DeleteFilesByDirectory (context. getExternalCacheDir ());
  31. }
  32. }
  33. /*** 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 */
  34. Public static void cleanCustomCache (String filePath ){
  35. DeleteFilesByDirectory (new File (filePath ));
  36. }
  37. /*** Clear all data of this application ** @ param context * @ param filepath */
  38. Public static void cleanApplicationData (Context context, String... filepath ){
  39. CleanInternalCache (context );
  40. CleanExternalCache (context );
  41. CleanDatabases (context );
  42. CleanSharedPreference (context );
  43. CleanFiles (context );
  44. For (String filePath: filepath ){
  45. CleanCustomCache (filePath );
  46. }
  47. }
  48. /*** Delete method: Only files in a folder are deleted. If the imported directory is a file, no processing will be performed. *** @ param directory */
  49. Private static void deleteFilesByDirectory (File directory ){
  50. If (directory! = Null & directory. exists () & directory. isDirectory ()){
  51. For (File item: directory. listFiles ()){
  52. Item. delete ();
  53. }
  54. }
  55. }
  56. }

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.