清理/缓存分区

时间:2018-03-07 09:37:37

标签: android permissions root

我正在尝试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

screen from android studio

然后我调用了新的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 studioadb看到呼叫时,该文件已被删除。

enter image description here

但是,当我试图定义我有多少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 cacheadb

我打电话

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 filesno 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

0 个答案:

没有答案