我正在尝试clean /cache
android tv
的{{1}}文件夹。
一开始,当我tv
启动时,我调用service
,使用此方法在此文件夹中定义space e valuable
。
public static long getCacheFreeSize() {
StatFs stat = new StatFs("/cache");
DebugUtils.logV(TAG, "all space of cache " + stat.getTotalBytes() / 1024 + "kb");
DebugUtils.logV(TAG, "free cache " + stat.getBlockSizeLong() * stat.getAvailableBlocksLong() / 1024 + "kb");
return stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
}
所以当tv starts
我看到logs
:
V/CommonUtils: all space of cache 1015704kb
V/CommonUtils: free cache 999084kb
在apps
之后的其他tv
创建新
file
/cache/update_s.zip
然后我调用了新的method
,它应该是clean cache
文件夹。
public static Completable deleteAppsCache(Context context, long spaceToClear) {
return Completable.create(emitter -> {
boolean executed = execCommand("chmod", "775", "/cache");
StatFs stat = new StatFs("/cache");
DebugUtils.logV(TAG, "all space of cache " + stat.getTotalBytes() / 1024 / 1024 + " mb " + "chmod 775 executed? "+ executed);
DebugUtils.logV(TAG, "trying to clear some space in cache ");
if (CommonUtils.removeFile(CommonUtils.getTVCacheFile())) {
DebugUtils.logV(TAG, " tv cache removed, free space: " + CommonUtils.getCacheFreeSize());
}
if (CommonUtils.removeFile(CommonUtils.getCacheFile())) {
DebugUtils.logV(TAG, "cache signed removed, free space: " + CommonUtils.getCacheFreeSize());
}
long size = CommonUtils.getCacheFreeSize();
DebugUtils.logV(TAG, "cache cleared now size is " + size / 1024 + " kb");
if (CommonUtils.isFreeSpaceCache(spaceToClear)) {
emitter.onComplete();
}
List<File> files = (List<File>) FileUtils.listFiles(new File("cache/"), TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (int i = 0; i < files.size(); i++) {
CommonUtils.removeFile(files.get(i));
}
if (size > spaceToClear) {
emitter.onComplete();
} else {
final PackageManager pm = context.getPackageManager();
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals("freeStorage")) {
Class[] params = m.getParameterTypes();
if (params.length == 2) {
// Found the method I want to use
try {
DebugUtils.logV(TAG, "trying free " + spaceToClear + " bytes from cache");
long desiredFreeStorage = spaceToClear;
m.invoke(pm, desiredFreeStorage, null);
} catch (Exception e) {
DebugUtils.logV(TAG, "exception during cleaning cache 1 " + e.getMessage());
}
break;
}
}
}
}
emitter.onComplete();
});
}
现在我可以从android studio
和adb
看到呼叫时,该文件已被删除。
但是,当我试图定义我有多少space
时,我仍然会变老information
。
这是logs
:
V: all space of cache 991 mb chmod 775 executed? true
V: trying to clear some space in cache
V: not removed: /cache/funtvupgrade/update.zip
V: removed: /cache/update_signed.zip
V: all space of cache 1015704kb
V: free cache 496400kb
V: cache signed removed, free space: 508313600
V: all space of cache 1015704kb
V: free cache 496400kb
V: cache cleared now size is 496400 kb
V: all space of cache 1015704kb
V: free cache 496400kb
V: removed: /cache/update_s.zip
V: cache cleaned
V: all space of cache 1015704kb
V: free cache 496400kb
所以我正试着clean cache
与adb
。
我打电话
adb root
adb connect -my ip
adb shell
su
mount -o rw,remount,rw /cache
cd /cache
rm -Rr -f *
此代码cleans
所有cache
文件夹,我从android studio
看到所有folder
为空,no files
,no directories
。
然后我可以调用我的方法,我发现我没有space
:
all space of cache 1015704kb
V: free cache 496408kb
在data
之后,我感觉某些invalidated
不是cache cleaning
。
并且android.os.StatFs
错误地提供了space
。
所以这就是问题:
我做错了什么,clean cache
以编程方式正确的方法是什么?
我使用过的链接: Clear Cache in Android Application programmatically