我正在开发一个应用,我需要在 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();
}
}