Android Development tutorial clears the Android data caching example (clears the local data cache) _android

Source: Internet
Author: User

Copy Code code as follows:

* * File Name: Datacleanmanager.java * Description: The main function is to clear the internal/external cache, clear the database, clear the sharedpreference, clear the files and clear the custom directory * *

Import Java.io.File;
Import Android.content.Context;
Import android.os.Environment;

/** * The Application Data Removal Manager * *
public class Datacleanmanager {
/** * Clear this application 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 the application 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 content under/data/data/com.xxx.xxx/files * * @param context * *
public static void Cleanfiles (context context) {
Deletefilesbydirectory (Context.getfilesdir ());
}

/**
* * Clear the external cache content (/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 custom path of the file, use caution, please do not accidentally deleted. And only file deletion in directory is supported * * @param filePath * *
public static void Cleancustomcache (String filePath) {
Deletefilesbydirectory (New File (FilePath));
}

   /** * Erase all data in this application * * @param context * @param filepath */
    public static void CL Eanapplicationdata (Context, String ... filepath) {
        Cleaninternalcache (context);
        Cleanexternalcache (context);
        cleandatabases (context);
        cleansharedpreference (context);
        cleanfiles (context);
        for (String filepath:filepath) {
             Cleancustomcache (FilePath);
       }
   }

/** * Delete method only files under a folder are deleted here, and 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 ();
}
}
}
}

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.