Share: Android clears local data cache code

Source: Internet
Author: User

Tag:android   import   databases    Database    files    

/*  *    pieces  : datacleanmanager.java  *  description      Description:    main functions are to clear the inside/out cache, clear the database, clear the sharedpreference, clear files and clear the custom directory   */ import java.io.File; import android.content.context;import android.os.environment; /** *  the application Data Purge manager  * /publicclass datacleanmanager {    /** *  clear this app internal cache (/data/data/ Com.xxx.xxx/cache)  * *  @paramcontext  */    publicstaticvoid  Cleaninternalcache (Context context)  {        Deletefilesbydirectory (Context.getcachedir ());    }     /**  *  Clear all databases for this application (/data/data/com.xxx.xxx/databases)  * *  @param  context */     publicstaticvoid cleandatabases (Context context)  {        deletefilesbydirectory (New filE ("/data/data/"                  + context.getpackagename ()  +  "/databases"));    }      /**     **  Clear this application sharedpreference (/data/data/com.xxx.xxx/shared_prefs ) * *  @param      *context     */     publicstaticvoid cleansharedpreference (Context context)  {        deletefilesbydirectory (New file ("/data/data/"                  + context.getpackagename ()  + "/shared_prefs"));     }     /** *  clear the application database by name  * *  @param   context *  @paramdbName  */    publicstaticvoid cleandatabasebyname ( context context,  Stringdbname)  {       context.deletedatabase (dbName);     }     /** *  clear the content under/data/data/com.xxx.xxx/files  * *   @param  context */    publicstaticvoid cleanfiles (Context context )  {       deletefilesbydirectory (Context.getfilesdir ());     }     /**     **  clear the contents of the external cache (/mnt/sdcard/ Android/data/com.xxx.xxx/cache)  * *  @param      *context      */    publicstaticvoid cleanexternalcache (Context context)  {       if (Environment.getexternalstoragestate (). Equals (                 environment.media_mounted) )  {   &Nbsp;       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 */    publicstaticvoid  Cleancustomcache (String filepath)  {       deletefilesbydirectory ( New file (FilePath));    }     /** *  Erase all data from this app  * *  @param  context *  @paramfilepath  */     Publicstaticvoid cleanapplicationdata (Context context,string... filepath)  {        cleaninternalcache (context);        Cleanexternalcache (context);        cleandatabases (context);       &nbSp;cleansharedpreference (context);        cleanfiles (context);        for  (String filepath : filepath)  {            cleancustomcache (FilePath);        }    }     /** *  Delete method   This will only delete files under a folder, If the incoming directory is a file, it will not be processed  * *  @param  directory */     Privatestaticvoid deletefilesbydirectory (file directory)  {        if  (directory != null&& directory.exists ()  &&  Directory.isdirectory ())  {           for  ( File item : directory.listfiles ())  {                 item.deLete ();           }        }    }}


for Performance testing for apps I usually use:www.ineice.com


Share: Android clears local data cache code

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.