Android 实现清除缓存

1.计算出缓存的大小  显示在相应TextView上

/**
 * 获取缓存大小
 */
String totalCacheSize = null;
try {
    totalCacheSize = DataCleanManager.getTotalCacheSize(getActivity());
} catch (Exception e) {
    e.printStackTrace();
}
TextView.setText(totalCacheSize);

2.点击相应TextView 弹出AlertLog

new AlertDialog.Builder(getActivity())
        .setTitle("清除缓存")
        .setMessage("是否确认清除缓存")
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
		//清除缓存的方法
                DataCleanManager.clearAllCache(getActivity());
                settingNumber.setText("0M");
                Toast.makeText(getActivity(), "清除成功", Toast.LENGTH_LONG).show();
            }
        })
        .setNegativeButton("取消", null)
        .show();

//因为不仅只有MainActivity一个页面,总会有MainActivity被覆盖的时候,所以我们需要在MainActivity走onResume生命周期的时候,再次计算缓存大小。

@Override
public void onResume() {
    super.onResume();
    /**
     * 获取缓存大小
     */
    String totalCacheSize = null;
    try {
        totalCacheSize = DataCleanManager.getTotalCacheSize(getActivity());
    } catch (Exception e) {
        e.printStackTrace();
    }
    settingNumber.setText(totalCacheSize);
}

想了解更多?

                        https://blog.csdn.net/as89751      

 

猜你喜欢

转载自blog.csdn.net/as89751/article/details/82353015