Android clears local data cache code

Source: Internet
Author: User

Main features clear internal/external cache, clear database, clear sharedpreference, purge files and clear custom directories

/* * File Name: Datacleanmanager.java * Description: The main function is to clear the internal/external cache, clear the database, clear the sharedpreference, clear files and clear the custom directory */import java.io.Fi Le;import Android.content.context;import android.os.environment;/** * This app data Purge 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 stat IC void Cleansharedpreference (context context) {Deletefilesbydirectory (New File ("/data/data/" + con    Text.getpackagename () + "/shared_prefs"); /** * Clear the application database by name * * @param cOntext * @param dbName */public static void Cleandatabasebyname (context context, String DbName) {Context.delet    Edatabase (DbName);        /** * Clear the contents under/data/data/com.xxx.xxx/files * * @param context */public static void Cleanfiles (context context) {    Deletefilesbydirectory (Context.getfilesdir ()); }/** * * Clears the contents of the external cache (/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param * Context */public Stati c void Cleanexternalcache (context context) {if (Environment.getexternalstoragestate (). Equals (EnviR Onment.        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) {Deletefilesbydirecto    Ry (New File (FilePath)); /** * Clears all data from 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 will only delete files under a folder, if the incoming directory is a file, will not do processing * * @param directory */private static void Deletefilesby  Directory (File directory) {if (directory! = null && directory.exists () && directory.isdirectory ())            {for (File item:directory.listFiles ()) {item.delete (); }        }    }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android clears local data cache code

Related Article

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.