在我的应用程序中,我已经注册了一个广播接收器来接收系统意图ACTION_DEVICE_STORAGE_LOW。我希望只要手机内存不足,就会播放此内容。所以,我下载了一些额外的应用程序(我的手机有一个非常小的内存),导致操作系统显示系统内存不足的通知。电话剩余10-15MB。但是,我的广播接收机从未收到过这个意图。然而,系统通知留在通知栏中,并且由于内存较低,我无法浏览互联网。何时广播ACTION_DEVICE_STORAGE_LOW意图?
只要显示低内存通知,是否应该广播这个意图?还是有一些甚至更低的内存阈值,会发出这种广播,我还没有打到我的手机?在文档中它只说“广播动作:一种粘滞的广播,表明设备上的内存不足”。由于它实际上并没有定义“低内存条件”,我不知道我是否做错了什么,或者如果我还没有达到那种条件。
这里是我的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%的内部存储空间发送的,但我仍然无法接收广播。 – mattgmg1990
如果有人感兴趣,我找到了接收广播的问题的答案。事实证明,这个意图匹配默认的datascheme(意思是不要添加任何数据方案到意图过滤器)。然后它与没有问题的操作相匹配。我创建了两个过滤器来匹配我计划收到的三个操作,因为ACTION_MEDIA_MOUNTED需要添加文件datascheme。 – mattgmg1990