【发布时间】:2021-07-30 08:09:18
【问题描述】:
在我的应用程序中,我注册了一个广播接收器来接收系统意图 ACTION_DEVICE_STORAGE_LOW。我希望每当手机内存不足时都会播放此消息。因此,我下载了一些额外的应用程序(我的手机内存非常小),导致操作系统显示系统内存不足的通知。手机剩余 10 - 15 MB。但是,我的广播接收器从未收到过这种意图。但是系统通知一直停留在通知栏中,由于内存不足,我无法浏览互联网。
是否应在显示内存不足通知时广播此意图?或者是否有一些更低的内存阈值可以发送我尚未在手机上点击的广播?在文档中,它只显示“广播操作:指示设备内存不足的粘性广播”。由于它实际上并没有定义“低内存条件”,我不知道我是否做错了什么,或者我只是还没有达到那个条件。
这是我的 BroadcastReceiver 的代码:
public class MemoryBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
Log.isExternalStorageProblem = false;
Log.clearNotification(Log.externalMemoryNotificationID);
}
if (action.equals(Intent.ACTION_DEVICE_STORAGE_OK)) {
Log.isInternalStorageLow = false;
Log.clearNotification(Log.internalMemoryNotificationID);
}
if (action.equals(Intent.ACTION_DEVICE_STORAGE_LOW)) {
Log.isInternalStorageLow = true;
Log.displayMemoryNotification("Internal Storage Low",
"The internal storage is low, Please fix this.",
"Please clear cache and/or uninstall apps.", Log.internalMemoryNotificationID);
}
}
}
我有一个服务来初始化接收器,添加意图过滤器并注册它(除其他外):
private MemoryBroadcastReceiver memoryBroadcastReciever = new MemoryBroadcastReceiver();
public void registerBroadcastReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
filter.addDataScheme("file");
this.getApplicationContext().registerReceiver(memoryBroadcastReciever, filter);
}
@Override
public void onCreate() {
registerBroadcastReceiver();
}
【问题讨论】:
-
我刚刚发布了我的代码。 DallaRosa 的回答很棒,我认为他是正确的关于以 10% 的内部存储空间发送广播,但我仍然无法接收广播。
-
如果有人感兴趣,我找到了接收广播问题的答案。事实证明,此意图与默认数据方案匹配(意味着不向意图过滤器添加任何数据方案)。然后它匹配动作没有问题。我创建了两个过滤器来匹配我计划接收的三个操作,因为 ACTION_MEDIA_MOUNTED 需要添加文件数据方案。