如何在Android Oreo API 26及更高版本上检查设备的存储空间是否不足 [英] How to check if the device has Low Storage on Android Oreo API 26 and Above

查看:63
本文介绍了如何在Android Oreo API 26及更高版本上检查设备的存储空间是否不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查设备在Android 8 Oreo上的存储空间是否不足.我在Android文档中看到 Intent.ACTION_DEVICE_STORAGE_LOW 在API 26中已弃用.

How can I check if the device has low storage on Android 8 Oreo. I saw in the Android Documentation that the Intent.ACTION_DEVICE_STORAGE_LOW is deprecated in API 26.

此常数在API级别26中已弃用.如果您的应用定位到O或更高级别,则此广播将不再传递到清单中定义的任何BroadcastReceiver.相反,强烈建议应用使用改进的getCacheDir()行为,以便系统可以在需要时自动释放存储.-Android文档

This constant was deprecated in API level 26. if your app targets O or above, this broadcast will no longer be delivered to any BroadcastReceiver defined in your manifest. Instead, apps are strongly encouraged to use the improved getCacheDir() behavior so the system can automatically free up storage when needed. - Android Documentation

他们鼓励我改用getCacheDir().

They are encouraging me use getCacheDir() instead.

但是我不太了解它,因为getCacheDir()似乎将系统缓存目录路径作为FILE对象返回,该文件只能用于清除缓存等.

But I don't understand much of it, as getCacheDir() seems to return the system cache directory path as a FILE object, which can only be used to clear cache or some such.

但是我需要检查设备存储空间是否不足.我希望有人能在这方面帮助我

But I need to check whether the device is running low on device storage. I hope someone will help me in this

推荐答案

正如问题中正确指出的,API 26

As correctly stated in the question, the API 26 Intent.ACTION_DEVICE_STORAGE_LOW is deprecated and Context#getCacheDir() is advised to be used instead to free up space from you application's cache.

此操作存在多个问题(下文列举),但首先:请注意,保持高速缓存合理小"(例如1 MB)是一种很好的做法,我引用:

There are multiple problems with this (enumerated below), but first: note that it is good practice to keep cache 'reasonably small' (e.g. 1 MB), I quote:

getCacheDir()

返回一个文件,该文件代表应用程序的临时缓存文件的内部目录.请确保在不再需要每个文件后将其删除,并对在任何给定时间使用的内存量实施合理的大小限制,例如1MB.

Returns a File representing an internal directory for your app's temporary cache files. Be sure to delete each file once it is no longer needed and implement a reasonable size limit for the amount of memory you use at any given time, such as 1MB.

警告:如果系统存储空间不足,则可能会在没有警告的情况下删除您的缓存文件.

Caution: If the system runs low on storage, it may delete your cache files without warning.

(源)

因此,这里存在三个问题:

So, there are three problems here:

  1. 我们应该清除缓存,但是它可能已经相当小(例如1 MB),因此清除缓存可能不会释放足够的空间,使空闲存储再次变为OK(类似于已弃用的因此,清除缓存似乎无济于事,所以我将不介绍如何执行此操作的详细信息.

    So, clearing the cache doesn't seem to help, so I won't go into the details of how to do that.

    但是,按照此答案,我们可以假设系统在空闲存储空间为10%时进入了我们要检测的低存储状态.根据链接的答案,这个数字是Android的默认值,但是并没有阻止设备制造商(或ROM开发人员)更改它.

    However, as per this answer, we could assume that at 10% free storage the system enters the low storage state that we want to detect. This number is Android's default, but there's little preventing a device manufacturer (or ROM developer) from changing it, according to the linked answer.

    在这一点上,对我来说,这10%是一个神奇的数字,我想知道是否可以通过编程确定该阈值.如果您知道怎么做,请编辑我的答案,自己发布答案或对我的答案发表评论.

    要使用 getCacheDir()进行此操作,可以使用以下代码:

    To do this using getCacheDir(), you could use the following:

    Java,来自 Context (例如 Activity ):

    Java, from a Context (e.g. Activity):

    File cacheDir = getCacheDir();
    if (cacheDir.getUsableSpace() * 100 / cacheDir.getTotalSpace() <= 10) { // Alternatively, use cacheDir.getFreeSpace()
      // Handle storage low state
    } else {
      // Handle storage ok state
    }
    

    科特琳,来自 Context (例如 Activity ):

    Kotlin, from a Context (e.g. Activity):

    if (cacheDir.usableSpace * 100 / cacheDir.totalSpace <= 10) { // Alternatively, use cacheDir.freeSpace
      // Handle storage low state
    } else {
      // Handle storage ok state
    }
    

    现在,是使用可用空间还是可用空间,这对我来说还不是很清楚.差异在此处进行了描述.

    Now, whether to use the usable space or free space, that's not entirely clear to me. The difference is described here.

    深入研究Android源代码后,我发现无法访问我的代码的系统服务,该服务会检查存储空间是否不足:

    Diving into the Android source I found a system service, that I cannot access in my code, that checks for low storage: DeviceStorageMonitorService. It gets its lowBytes variable from StorageManager#getStorageLowBytes, which I cannot access either. If that would be possible in some non-hacky way, that would be a way to get the low storage bytes threshold. There you see the source uses getUsableSpace(), so that's why I chose that instead of getFreeSpace() too for my code snippets.

    这篇关于如何在Android Oreo API 26及更高版本上检查设备的存储空间是否不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆