Clear all application caches

Source: Internet
Author: User
/** File name: datacleanmanager. java * Description: The main functions are to clear the internal/external cache, clear the database, clear sharedpreference, clear files, and clear the custom directory */package COM. test. dataclean; import Java. io. file; import android. content. context; import android. OS. environment;/***** the application data clearing manager */public class datacleanmanager {/***** clears the internal cache of the application (/data/COM. xxx. xxx/cache) ** @ Param context */public static void cleaninternalcache (context) {deletefilesbydirecto Ry (context. getcachedir ();}/*** clear all databases of the application (/data/COM. xxx. xxx/databases) ** @ Param context */public static void cleandatabases (context) {deletefilesbydirectory (new file ("/data/" + context. getpackagename () + "/databases");}/*** clear the sharedpreference (/data/COM. xxx. xxx/shared_prefs) ** @ Param context */public static void cleansharedpreference (context) {delet Efilesbydirectory (new file ("/data/" + context. getpackagename () + "/shared_prefs "));} /*** clear the application database by name ** @ Param context * @ Param dbname */public static void cleandatabasebyname (context, string dbname) {context. deletedatabase (dbname);}/*** clear/data/COM. xxx. content in xxx/Files ** @ Param context */public static void cleanfiles (context) {deletefilesbydirectory (context. get Filesdir ();}/*** clear content in the external cache (/mnt/sdcard/Android/data/COM. xxx. xxx/cache) ** @ Param context */public static void cleanexternalcache (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, string... filepath) {cleaninternalcache (context); cleanexternalcache (context); cleandatabases (context); cleansharedpreference (context); cleanfiles (C Ontext); For (string filepath: filepath) {cleancustomcache (filepath) ;}}/*** the deletion method only deletes files in a folder, 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 ();}}}}

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.