在Android应用程序清除缓存 [英] Clear cache in android application

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

问题描述

我不认为这是一个重复的。好吧,我解释我需要什么。我已经得到了安装在我的设备中的所有应用程序的列表。通过点击我需要显示一个对话框,说:你想清除缓存?用是或当然的,没有。我发现这个教程:的http://android-sample-$c$c.blogspot.it/2012/01/how-to-clear-cache-data-in-android.html但似乎删除数据文件夹。我想知道的是,有区别吗?是否有只为明确的缓存code,而不是一个应用程序的数据?

code:

 保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
        super.onListItemClick(L,V,位置ID);
        /**清除缓存*/
        软件包管理系统下午= getPackageManager();
        //获取的软件包管理系统的所有方法
        方法[] =方法pm.getClass()getDeclaredMethods()。
        为(M法:方法){
            如果(m.getName()。等于(freeStorage)){
                //找到我想要使用的方法
                尝试{
                    长desiredFreeStorage = 8 * 1024 * 1024 * 1024; //请求的可用空间8GB
                    m.invoke(时许,desiredFreeStorage,NULL);
                }赶上(例外五){
                    //方法调用失败。可能是一个权限问题
                }
                打破;
            }
        }
    }


解决方案

也许这将帮助你:

 软件包管理系统PM = getPackageManager();
//获取的软件包管理系统的所有方法
方法[] =方法pm.getClass()getDeclaredMethods()。
为(M法:方法){
    如果(m.getName()。等于(freeStorage)){
        //找到我想要使用的方法
        尝试{
            长desiredFreeStorage = 8 * 1024 * 1024 * 1024; //请求的可用空间8GB
            m.invoke(时许,desiredFreeStorage,NULL);
        }赶上(例外五){
            //方法调用失败。可能是一个权限问题
        }
        打破;
    }
}

不要忘了权限:

 <使用许可权的android:NAME =android.permission.CLEAR_APP_CACHE/>

I don't think it's a duplicate. Well, I explain what I need.. I've got a list of all application installed in my device.. By a click I need to show a dialog that says "Do you want clear cache?" with "yes" or of course, "no". I found this tutorial: http://android-sample-code.blogspot.it/2012/01/how-to-clear-cache-data-in-android.html but seems to delete the data folder. What I want to know is; is there difference? Is there a code only for clear cache and not data of a application?

Code:

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);    
        /**Clear cache*/
        PackageManager  pm = getPackageManager();
        // Get all methods on the PackageManager
        Method[] methods = pm.getClass().getDeclaredMethods();
        for (Method m : methods) {
            if (m.getName().equals("freeStorage")) {
                // Found the method I want to use
                try {
                    long desiredFreeStorage = 8 * 1024 * 1024 * 1024; // Request for 8GB of free space
                    m.invoke(pm, desiredFreeStorage , null);
                } catch (Exception e) {
                    // Method invocation failed. Could be a permission problem
                }
                break;
            }
        }
    }

解决方案

May this will help you:

PackageManager  pm = getPackageManager();
// Get all methods on the PackageManager
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m : methods) {
    if (m.getName().equals("freeStorage")) {
        // Found the method I want to use
        try {
            long desiredFreeStorage = 8 * 1024 * 1024 * 1024; // Request for 8GB of free space
            m.invoke(pm, desiredFreeStorage , null);
        } catch (Exception e) {
            // Method invocation failed. Could be a permission problem
        }
        break;
    }
}

Dont forgot permission :

<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>

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

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