在android中显示总缓存大小

时间:2016-01-05 04:27:40

标签: android caching

我正在开发一个应用,我需要在 TextView 中显示所有应用组合的总缓存。我该怎么做?

我找到了这个代码段from an answer

PackageManager packageManager = getPackageManager();
List<ApplicationInfo> packages =  packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) 
{
try 
{
    Context mContext = createPackageContext(packageInfo.packageName, CONTEXT_IGNORE_SECURITY);
    File cacheDirectory = mContext.getCacheDir();
    if(cacheDirectory==null)
    {
        cacheArrayList.add("0");
    }
    else
    {
        cacheArrayList.add(String.valueOf(cacheDirectory.length()/1024));
    }
}
catch (NameNotFoundException e)
{
    e.printStackTrace();
}
}

0 个答案:

没有答案