We usually integrate third-party SDK in the app. Sometimes, when the app is used for a long time, the database in the app will be too full, which leads to the application of CARsh. Then we have to think of a solution, the idea is as follows
1. Scan the SDK directory where the database is stored, which is the package name /data/data
2, and then from the subdirectory to find, if the db file is directly deleted
/* Clear the cache database * * @desc clear it once a day */
private void cleanDBData(a) {
SharedPreferences sharedPreferences = getSharedPreferences("clear_data_recode", MODE_PRIVATE);
String data = sharedPreferences.getString("date"."");
String currentDate = DateUtils.date2String(System.currentTimeMillis());
if (data.equals(currentDate)) {
return;
}
String packageName = getPackageName();
File fileRoot = new File("data/data/" + packageName);
try {
if (fileRoot.exists() || fileRoot.isDirectory()) {
File[] files = fileRoot.listFiles();
if (files == null) {
return;
}
for (int i = 0; i < files.length; i++) {
//Log.i("data_file",files[i].getName());
String name = files[i].getName();
if (("databases".equals(name) || "files".equals(name) && files[i].isDirectory())) {
File[] filesLast = files[i].listFiles();
if (filesLast == null) {
break;
}
for (File file : filesLast) {
// The file exists in a non-directory
if(file ! =null&& file.exists() && ! file.isDirectory() && file.getPath().contains(".db")) {
Log.i("data_file", file.getAbsolutePath() + ",size=" + file.length());
file.delete();
}
}
SharedPreferences spSave = getSharedPreferences("clear_data_recode", MODE_PRIVATE);
SharedPreferences.Editor editor = spSave.edit();
editor.putString("date", DateUtils.date2String(System.currentTimeMillis())); editor.commit(); }}}}catch(Exception e) { System.out.println(e.getMessage()); }}Copy the code