Share: Android clears local data cache code

Source: Internet
Author: User

/*  *    Pieces   Name:  datacleanmanager.java  *  description      :   main functions are to clear the internal/external 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; /** *  This app data Cleanup 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 app (/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)  {        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 content under 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, 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  * *  @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 are deleted, if the incoming directory is a file, it 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 ();             }         }    }}


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.