清除机器人缓存 [英] Clearing android cache

查看:219
本文介绍了清除机器人缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我已经得到了约为1 / 2MB一个code尺寸的应用程序。该应用程序包括web视图用于显示多个页面。因此,我的缓存结束于2,5mb。不是很多,但足够了。我如何才能清除缓存的onDestroy?

Hi I've got an app with a code size of approximately 1/2mb. The app includes a webview for showing several pages. My cache therefore ends up at 2,5mb. Not much but enough. How can I clear my cache onDestroy?

THX!

推荐答案

把这个code中的onDestroy()为明确的应用程序缓存

put this code in onDestroy() for clear app cache

@Override
    protected void onDestroy() {
        super.onDestroy();

        try {
            trimCache(this);
           // Toast.makeText(this,"onDestroy " ,Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public static void trimCache(Context context) {
        try {
           File dir = context.getCacheDir();
           if (dir != null && dir.isDirectory()) {
              deleteDir(dir);
           }
        } catch (Exception e) {
           // TODO: handle exception
        }
     }

     public 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;
              }
           }
        }

        // The directory is now empty so delete it
        return dir.delete();
     }

这篇关于清除机器人缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆