Android clear Local data cache code case

Source: Internet
Author: User

Android clear Local data cache code case

Directly on the code:

/*  * FileName:  datacleanmanager.java  * Description     Description:  Main functions are to clear the internal/external cache, clear the database, Clear sharedpreference, clear files and clear custom directories   */  Package com.test.dataclean;    Import Java.io.File ;   Import android.content.context;  Import android.os.environment;    /**  * This app data Removal manager   */   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 for this 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) { & nbsp;       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 the 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 &Nbsp;     *       * @param context      * @param filepath      */    public static void Cleanapplicationdata (Context Context, String ... filepath) {          cleaninternalcache (context);          cleanexternalcache (context);          cleandatabases (context);           Cleansharedpreference (context);         cleanfiles (context);            for (String filepath:filepath) {           cleancustomcache (filePath);       }       }       /**      * Delete method only the files under a folder will be deleted. If the incoming DirecTory is a file that will not be processed       *       * @param directory       */   private static void Deletefilesbydirectory (File directory) {        if (Directory! = null && directory.exists () && directory.isdirectory ()) {         for (File item:directory.listFiles ()) {               item.delete ();               }          }       }   }

Android clears local data cache code case

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.