How android receives DEVICE_STORAGE_LOW (insufficient storage space) broadcast | Reasons why DEVICE_STORAGE_OK broadcast is slow

Preface

When the storage space is insufficient, the Android system will send a broadcast indicating insufficient storage space, and a message will pop up in the notification bar. Ordinary applications can capture this broadcast to know that the system is in a low storage state.
However, during the actual test, it was found that it takes a while to receive android.intent.action.DEVICE_STORAGE_LOWthese android.intent.action.DEVICE_STORAGE_OKtwo broadcasts. In particular DEVICE_STORAGE_OK, the waiting time is even longer, about ten seconds.



How to monitor DEVICE_STORAGE_LOW broadcast?

Method 1: Register static broadcast

AndroidManifest.xmlAdd the following content to the file

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    ...此处内容省略...
    >
    
	<!-- receiver中的内容是广播相关的 -->
	<receiver
	    android:name=".MyStorageLOW"
	    android:enabled="true"
	    android:exported="true">
	    <intent-filter>
	        <action android:name="android.intent.action.DEVICE_STORAGE_LOW" />
	    </intent-filter>
	</receiver>

</application>

Create new class, inheritBroadcastReceiver

public class MyStorageLOW extends BroadcastReceiver {
    
    

    @Override
    public void onReceive(Context context, Intent intent) {
    
    
        //TODO:收到存储空间不足的广播时,此方法会被调用
        //TODO:此处插入一些操作...
    }
}

Method 2: Register for dynamic broadcast

BroadcastReceiver receiver;

receiver = new BroadcastReceiver() {
    
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
    
        if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(intent.getAction())) {
    
    
            //TODO:收到存储空间不足的广播
        }
    }
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
registerReceiver(receiver, filter); //记得调用 unregisterReceiver(receiver) 取消注册

illustrate

Although Android O will prompt that the method is outdated, and the method will be marked with a horizontal line as a warning, I can receive it when I test it on the Android 11 emulator, so I don’t have to worry about this function failing for the time being.
Method obsolete



How do I modify the threshold or range for low storage prompts?

Android's default trigger conditions for insufficient storage space are: 1. Less than 5% of the total storage; 2. Less than 500MB. Choose the one with the smallest capacity between the two conditions . For example, if your mobile phone has 64GB, the trigger condition will be 500MB. If your mobile phone has only 8GB storage, the trigger condition will be: 8GB * 5% ≈ 400MB. If you want to modify this restriction, you can modify the class under frameworksStorageManager :

路径:frameworks\base\core\java\android\os\storage\StorageManager.java
...
private static final int DEFAULT_THRESHOLD_PERCENTAGE = 5;  //修改此处的值可调整百分比
private static final long DEFAULT_THRESHOLD_MAX_BYTES = DataUnit.MEBIBYTES.toBytes(500); //修改此处的值可调整大小,单位byte
....

Then search for variables: DEFAULT_THRESHOLD_PERCENTAGEand DEFAULT_THRESHOLD_MAX_BYTES, the former is a percentage and the latter is a fixed value, which correspond to the two trigger conditions mentioned above.



Reason for slow broadcast reception

In my actual testing, when the specified threshold is reached, the broadcast with insufficient storage space is sent quickly, but after deleting some files, the broadcast is received after a while DEVICE_STORAGE_OK. There are very few online posts about this issue. I gave a rough summary. The possible reasons are as follows:
Android has a storage scanning mechanism. When your storage space is insufficient, it will first perform some operations, such as trying to clean up some files to alleviate this phenomenon. If there is still insufficient space after executing the above processes, the broadcast will be sent. At the same time, Android will start scanning the current storage space every 60 seconds until the device is out of insufficient space .
DeviceStorageMonitorService
That is to say, assuming we delete the file at 40 seconds, the system will scan the device's storage space at 60 seconds, and the corresponding DEVICE_STORAGE_OKbroadcast will be sent after a delay of 20 seconds.

However, there is no clear explanation of this issue in the Android development documentation, so this inference is for reference only. If anyone knows the real reason, you can comment.



Reference blog

Android O version (Android 8.0) Reminder when storage space is low

Android Low Storage机制(DeviceStorageMonitorService)

分析DeviceStorageMonitorService

Android Service device storage space monitoring

Search DEFAULT_THRESHOLD_MAX_BYTESto DEFAULT_THRESHOLD_PERCENTAGEfind more related content

Guess you like

Origin blog.csdn.net/Guan_li_peng/article/details/127634728