清除缓存



清除缓存


Mainactivity


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lixin.todaynews.app.MyApplication;
import com.example.lixin.todaynews.utils.NetUtils;

import org.w3c.dom.Text;

import java.io.File;
import java.math.BigDecimal;

import cn.jpush.android.api.JPushInterface;

import static com.example.lixin.todaynews.R.id.total;

/**
 * Created by hua on 2017/8/9.
 */

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    private SharedPreferences.Editor edit;
    private TextView total;

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shezhi);
        CheckBox cc = (CheckBox) findViewById(R.id.cc);
        ImageView setting_back = (ImageView) findViewById(R.id.setting_back);
        TextView t5 = (TextView) findViewById(R.id.t5);
        t5.setOnClickListener(this);
        total = (TextView) findViewById(R.id.total);
        try {
            //去计算缓存大小
            String totalCacheSize = getTotalCacheSize();
            total.setText(totalCacheSize);

        } catch (Exception e) {
            e.printStackTrace();
        }
        setting_back.setOnClickListener(this);
        boolean key = getSharedPreferences("FLAG", MODE_PRIVATE).getBoolean("key", true);
        SharedPreferences sp = getSharedPreferences("FLAG",MODE_PRIVATE);
        edit = sp.edit();

        cc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    edit.putBoolean("key",true).commit();
                    JPushInterface.resumePush(SettingActivity.this);
                }else {
                    edit.putBoolean("key",false).commit();
                    JPushInterface.stopPush(SettingActivity.this);
                }
            }
        });
    if (key){
        cc.setChecked(true);
    }else {
        cc.setChecked(false);
    }

    }
    public void tiaoshi(View view){
        Toast.makeText(SettingActivity.this, NetUtils.getInstance().getBASE_URL(), Toast.LENGTH_SHORT).show();
    }
    public void onclick(View view){
        // TODO: 2017/8/10 做一个alertDialog
        String[] strings = {"最佳效果", "较省流量", "极省流量"};
        int mode = MyApplication.getAppcontext().getSharedPreferences(NetUtils.SP_NAME, Context.MODE_PRIVATE).getInt(NetUtils.PICTURE_LOAD_MODE_KEY, 0);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("wifi网络流量");
        builder.setSingleChoiceItems(strings, mode, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                 TODO: 2017/8/10  要保存我们的图片加载模式
                MyApplication.getAppcontext().getSharedPreferences(NetUtils.SP_NAME, Context.MODE_PRIVATE).edit().putInt(NetUtils.PICTURE_LOAD_MODE_KEY, which).commit();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.create().show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.setting_back:
                this.finish();
                break;
            case R.id.t5:
                showtotalSettingDialog();
                break;
        }
    }


    /**
     * 清理app的缓存 其实是清除的getCacheDir getExternalCacheDir这两个文件
     *
     * @param context
     */
    public static void clearAllCache(Context context) {
        deleteDir(context.getCacheDir());
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            deleteDir(context.getExternalCacheDir());
        }
    }

    /**
     * 删除一个文件夹
     *
     * @param dir
     * @return
     */
    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();
    }

    /**
     * 计算app的缓存大小其实计算的是 getCacheDir()这个filegetExternalCacheDir()这个file大小的和
     */
    public String getTotalCacheSize() throws Exception {
        long cacheSize = getFolderSize(this.getCacheDir());

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            cacheSize += getFolderSize(this.getExternalCacheDir());
        }
//        File cacheDir = StorageUtils.getOwnCacheDirectory(this, "universalimageloader/Cache");
        return getFormatSize(cacheSize);
    }

    /**
     * 计算文件夹的大小
     */
    public static long getFolderSize(File file) throws Exception {
        if (!file.exists()) {
            return 0;
        }
        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 String getFormatSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            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";
    }
    private void showtotalSettingDialog(){


        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(SettingActivity.this);
        builder.setMessage("是否清除缓存");
        builder.setNegativeButton("取消",null);
        builder.setPositiveButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //imageLoader清除缓存大小的放大
//                ImageLoader.getInstance().clearDiskCache();

                clearAllCache(SettingActivity.this);
                String totalCacheSize = null;
                try {
                    totalCacheSize = getTotalCacheSize();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                total.setText(totalCacheSize);
            }
        });
        builder.create().show();
    }
}

MyApplication

package com.example.lixin.todaynews.app;

import android.app.Application;
import android.content.Context;
import android.os.Environment;

import com.mob.MobSDK;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import org.xutils.x;

import java.io.File;

import cn.jpush.android.api.JPushInterface;

import static com.umeng.socialize.utils.DeviceConfig.context;

/**
 * Created by hua on 2017/8/2.
 */

public class MyApplication extends Application {
    {

        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
    }
    public static MyApplication myApplication;
    @Override
    public void onCreate() {
        super.onCreate();
        myApplication = this;
        x.Ext.init(this);
        UMShareAPI.get(this);
        Config.DEBUG = true;
        MobSDK.init(this, "1ff5b3afb1cee", "40b1c5ea90b2599b7ef04551a9042327");
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

//         String path = Environment.getExternalStorageDirectory().getPath()+"/"+"getCacheDir"+"/"+"Pictrues";
//         File file = new File(path);
//         String path1 = context.getExternalCacheDir()+"/getCacheDir/pictures";
//         File file = new File(path1);
           File file=new File(getCacheDir(),"pictures");
                 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                         .memoryCacheExtraOptions(400,800)//配置内存缓存图片的尺寸
                         //.diskCacheExtraOptions() bug 不推介你手动去配置
                         .memoryCacheSize(2 * 1024 * 1024)//配置内存缓存的大小  例如 : 2* 1024 * 1024 = 2MB
                         .threadPoolSize(3)//配置加载图片的线程数
                         .threadPriority(100)//配置线程的优先级
                         .diskCache(new UnlimitedDiskCache(file))//UnlimitedDiskCache 限制这个图片的缓存路径
                         .diskCacheSize(50 * 1024 * 1024)//sdcard缓存50MB
                         .diskCacheFileNameGenerator(new Md5FileNameGenerator())//MD5这种方式生成缓存文件的名字
                         .diskCacheFileCount(20)//配置sdcard缓存文件的数量
                         .build();//配置构建完成

                 ImageLoader.getInstance().init(config);

    }
    public static Context getAppcontext(){
        return myApplication;
    }
}

可以参考  http://www.2cto.com/kf/201503/385492.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值