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 ();
}
}
}
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.