是否可以选择删除Android M中所有应用或某些应用的缓存?
似乎大多数方式在Android M中不再起作用了.
作为参考,我在本讨论中使用了此代码: Android:清除所有应用程序的缓存?
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;
}
}
Run Code Online (Sandbox Code Playgroud)
结合此权限 …
如何获得应用程序的大小,包括AppData的大小?
目前我使用此代码,但它只获取APK大小.有没有办法获得AppData的大小,就像谷歌设置应用程序那样?
public String getApkSize(Context context, String packageName) {
try {
float value = new File(context.getPackageManager().getApplicationInfo(packageName, 0).publicSourceDir).length() / 1048576F;
if (value <= 0) {
value = 1;
}
return String.format("%.2f", value) + "MB";
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)