本應用數據清除管理器
DataCleanManager.java 是從網上摘的 忘了 名字了 對不住了
加載一個webview 產生緩存 衆所周知的webview是產生緩存的主要原因之一
webview加載之後 點擊按鈕 查詢緩存 然後清除緩存 再查詢緩存 可以看到 緩存確實被清除了
或者咋webview加載之後 點擊按鈕查詢緩存 然後去設置裏面應用程序 看程序的緩存是不是一樣的 答案肯定是一樣
下面是代碼
DataCleanManager.java
package com.yqy.yqy_cache;
/* * 文 件 名: DataCleanManager.java * 描 述: 主要功能有清除內/外緩存,清除數據庫,清除sharedPreference,清除files和清除自定義目錄 */
import java.io.File;
import java.math.BigDecimal;
import android.content.Context;
import android.os.Environment;
/** * 本應用數據清除管理器 */
public class DataCleanManager {
public static String getTotalCacheSize(Context context) throws Exception {
long cacheSize = getFolderSize(context.getCacheDir());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cacheSize += getFolderSize(context.getExternalCacheDir());
}
return getFormatSize(cacheSize);
}
// 獲取文件
//Context.getExternalFilesDir() --> SDCard/Android/data/你的應用的包名/files/ 目錄,一般放一些長時間保存的數據
//Context.getExternalCacheDir() --> SDCard/Android/data/你的應用包名/cache/目錄,一般存放臨時緩存數據
public static long getFolderSize(File file) throws Exception {
long size = 0;
try {
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
// 如果下面還有文件
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);
} else {
size = size + fileList[i].length();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
public static void clearAllCache(Context context) {
deleteDir(context.getCacheDir());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
deleteDir(context.getExternalCacheDir());
}
}
private static boolean deleteDir(File dir) {
if(dir == null){
return false;
}
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
/**
* 格式化單位
*
* @param size
* @return
*/
public static String getFormatSize(double size) {
double kiloByte = size / 1024;
if (kiloByte < 1) {
// return size + "Byte";
return "0K";
}
double megaByte = kiloByte / 1024;
if (megaByte < 1) {
BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "KB";
}
double gigaByte = megaByte / 1024;
if (gigaByte < 1) {
BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "MB";
}
double teraBytes = gigaByte / 1024;
if (teraBytes < 1) {
BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "GB";
}
BigDecimal result4 = new BigDecimal(teraBytes);
return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
+ "TB";
}
/** * 清除本應用內部緩存(/data/data/com.xxx.xxx/cache) * * @param context */
public static void cleanInternalCache(Context context) {
deleteFilesByDirectory(context.getCacheDir());
}
/** * 清除本應用所有數據庫(/data/data/com.xxx.xxx/databases) * * @param context */
public static void cleanDatabases(Context context) {
deleteFilesByDirectory(new File("/data/data/"
+ context.getPackageName() + "/databases"));
}
/**
* * 清除本應用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"));
}
/** * 按名字清除本應用數據庫 * * @param context * @param dbName */
public static void cleanDatabaseByName(Context context, String dbName) {
context.deleteDatabase(dbName);
}
/** * 清除/data/data/com.xxx.xxx/files下的內容 * * @param context */
public static void cleanFiles(Context context) {
deleteFilesByDirectory(context.getFilesDir());
}
/**
* * 清除外部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());
}
}
/** * 清除自定義路徑下的文件,使用需小心,請不要誤刪。而且只支持目錄下的文件刪除 * * @param filePath */
public static void cleanCustomCache(String filePath) {
deleteFilesByDirectory(new File(filePath));
}
/** * 清除本應用所有的數據 * * @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);
}
}
/** * 刪除方法 這裏只會刪除某個文件夾下的文件,如果傳入的directory是個文件,將不做處理 * * @param directory */
private static void deleteFilesByDirectory(File directory) {
if (directory != null && directory.exists() && directory.isDirectory()) {
for (File item : directory.listFiles()) {
item.delete();
}
}
}
}
MainActivity.java
package com.yqy.yqy_cache;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn_clear;
private WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_clear = (Button) findViewById(R.id.btn_clear);
wv = (WebView) findViewById(R.id.wv);
btn_clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
//查看緩存的大小
Log.e("YQY", DataCleanManager.getTotalCacheSize(MainActivity.this));
} catch (Exception e) {
e.printStackTrace();
}
//清除操作
DataCleanManager.clearAllCache(MainActivity.this);
try {
//清除後的操作
Log.e("YQY", DataCleanManager.getTotalCacheSize(MainActivity.this));
} catch (Exception e) {
e.printStackTrace();
}
}
});
wv.loadUrl("http://www.baidu.com");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="清除緩存" />
<WebView
android:id="@+id/wv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn_clear"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>