Android应用清除缓存一般清理目录和代码的教程
一个应用安装完成,会有缓存,缓存一般分为内部存储的和外置的,内存中路径为/data/data/pacgage_name/文件,先来看看一般缓存会存在哪些文件中:
(1)缓存文件夹:
/data/data/com.xx/cache
(2)sharepreferance文件夹:
/data/data/com.xx/shared_prefs
(3)db文件夹:
/data/data/com.xx/databases
(4)files文件夹:(指向同一文件夹)
context.getfilesdir()
/data/data/com.xx/files
(5)外置缓存文件夹:context.getexternalcachedir()
(6)lib文件夹,一般可以不清理该文件夹:
"/data/data/com.gtafe.zhpj/lib"
一般一个应用的缓存都存在这些文件夹中,接下直接上代码吧,把计算缓存和清除缓存都用一个类封装(带弹框):
[java] view plain copy
/**
* created by lan.zheng on 2016/9/1.
*/
public class cacheutil {
private static final string cache_data_db = "/data/data/com.gtafe.zhpj/databases";
private static final string cache_data_sf = "/data/data/com.gtafe.zhpj/shared_prefs";
private static final string cache_data_cache = "/data/data/com.gtafe.zhpj/cache";
private static file lfiledb;
private static file lfilesf;
private static file lfilec;
/**
* 获取所有缓存
* @param context
* @return
* @throws exception
*/
public static string gettotalcachesize(context context) throws exception {
lfiledb = new file(cache_data_db);
lfilesf = new file(cache_data_sf);
lfilec = new file(cache_data_cache);
//内存缓存db和sf,cache,files文件
long cachesizedb = getfoldersize(lfiledb);
long cachesizesf = getfoldersize(lfilesf);
long cachesizec = getfoldersize(lfilec);
long cachesize = cachesizedb + cachesizesf + cachesizec + getfoldersize(context.getfilesdir()); //context.getfilesdir()或者像上面一样直接用路径
//fresco产生的cache,如果需要就加入下面这行代码
//long cachesizefresco = fresco.getimagepipelinefactory().getmaindiskstoragecache().getsize();
cachesize = cachesize + cachesizefresco;
if (environment.getexternalstoragestate().equals(environment.media_mounted)) {
cachesize = cachesize + getfoldersize(context.getexternalcachedir());
}
return getformatsize(cachesize);
}
/**
* 清除所有缓存
* @param context
* @return
*/
static boolean isclearsuccess = true;
public static void clearallcache(context context,showclearresultlistener showclearresultlistener) {
mlistener = showclearresultlistener; //监听初始化
showwaitingdialog(context); //弹框等待
//清理fresco的缓存
imagepipeline imagepipeline = fresco.getimagepipeline();
imagepipeline.clearcaches();
//清理内存和文件缓存
isclearsuccess = deletedir(lfiledb);
isclearsuccess = deletedir(lfilesf);
isclearsuccess = deletedir(lfilec);
isclearsuccess = deletedir(context.getfilesdir());
if (environment.getexternalstoragestate().equals(environment.media_mounted)) {
isclearsuccess = deletedir(context.getexternalcachedir());
}
//todo 别的缓存
/* if(context.getcachedir() != null){
deletedir(context.getcachedir());
}*/
}
private static boolean deletedir(file dir) {
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();
}
// 获取文件
//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++) {
log.d("test11","file"+i+" = "+filelist[i].tostring());
// 如果下面还有文件
if (filelist[i].isdirectory()) {
size = size + getfoldersize(filelist[i]);
} else {
size = size + filelist[i].length();
}
}
} catch (exception e) {
e.printstacktrace();
}
return size;
}
/**
* 格式化单位
*
* @param size
* @return
*/
public static string getformatsize(double size) {
double kilobyte = size / 1024;
if (kilobyte < 1) {
// return size + "byte";
return "0.0m";
}
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";
}
/**
* 弹框确认方法
*/
public static void showwaitingdialog(context context){
//弹框
popupconfirmdialog(context , "清除中...", r.mipmap.ic_clear_cache);
}
private static dialog modifydialog;
static showclearresultlistener mlistener;
private static void popupconfirmdialog(context context, string content, int resid) {
//弹框内容
if (modifydialog == null) {
view contentview = layoutinflater.from(context).inflate(r.layout.dialog_waiting_for_opr, null);
textview modifycontent = (textview) contentview.findviewbyid(r.id.dialog_content_text);
modifycontent.settext(content);
imageview modifyimage = (imageview)contentview.findviewbyid(r.id.dialog_content_image);
modifyimage.setimagedrawable(context.getresources().getdrawable(resid));
modifydialog = new dialog(context, r.style.clearcachedialogtheme); //使用自定义的样式
modifydialog.setcanceledontouchoutside(false); //外围点击不消失
modifydialog.setcontentview(contentview);
}
modifydialog.show();
sleeptowaitclear();
}
static handler mhandler = new handler();
private static void sleeptowaitclear(){
mhandler.postdelayed(mrunnabledialogdismiss,1000); //延迟启动
}
private static runnable mrunnabledialogdismiss = new runnable() {
@override
public void run() {
if(modifydialog != null ){
modifydialog.dismiss();
modifydialog = null;
mlistener.clear(isclearsuccess);
}
}
};
/**
* 1秒后回调清除结果
*/
public interface showclearresultlistener{
void clear(boolean isclear);
}
}
弹框的样式调用:
[html] view plain copy
<!-- 清除缓存弹框样式 -->
<style name="clearcachedialogtheme" parent="android:theme.dialog">
<item name="android:windowframe">@null</item>
<item name="android:windowisfloating">true</item>
<item name="android:windowistranslucent">true</item>
<item name="android:windownotitle">true</item><!--除去title-->
<item name="android:backgrounddimenabled">true</item>
<item name="android:backgrounddimamount">0.3</item><!-- 背景灰度 -->
<item name="android:windowbackground">@color/transparent</item><!--除去背景色,透明-->
<item name="android:radius">8dp</item>
</style>
封装的弹框代码(圆角代码省略@drawable/dialog_content_radius):
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_content_radius"
android:paddingtop="@dimen/hdp_20.0"
android:paddingbottom="@dimen/hdp_20.0"
android:paddingleft="@dimen/wdp_20.0"
android:paddingright="@dimen/wdp_20.0"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center">
<imageview
android:paddingbottom="@dimen/hdp_20.0"
android:paddingleft="@dimen/wdp_80.0"
android:paddingright="@dimen/wdp_80.0"
android:id="@+id/dialog_content_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_clear_cache"/>
<textview
android:id="@+id/dialog_content_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxlines="5"
android:text="含笑半步癫"
android:textsize="@dimen/sp_16.0"
android:textcolor="@color/text_666666"
android:gravity="center"/>
</linearlayout>
最后就是在主函数中使用了:
(1)获取缓存大小:
[java] view plain copy
string cachesize=cacheutil.gettotalcachesize(this);
cache_size.settext(cachesize);
(2)清除缓存:
[java] view plain copy
cacheutil.clearallcache(usercenteractivity.this, new cacheutil.showclearresultlistener() {
@override
public void clear(boolean isclear) {
if(isclear){
cache_size.settext("0.0m");
}
}
});
以上就是清除缓存的方法,最后发现一个问题,在安卓4.4一下的中清除缓存后和管理器看到的结果是一样的,但是4.4以上的系统在管理器显示还是有些缓存没被清除,这个问题有人知道为什么吗?
上一篇: 2018Android开发大公司面试经验