mat*_*990 10 android broadcastreceiver
在我的应用程序中,我已注册广播接收器以接收系统意图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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个初始化接收器的服务,添加了intent过滤器并注册它(以及其他内容):
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();
}
Run Code Online (Sandbox Code Playgroud)
Dal*_*osa 24
TL; DR
只要设备制造商不更改默认设置,当可用内存达到内部设备内存的10%时,将广播意图.
长版
我通过这个意图的Android源代码,我得到了一个名为DeviceStorageMonitorService的类
(位于:frameworks/base/services/java/com/android/server/DeviceStorageMonitorService.java)
来自javadoc:
此类实现一项服务来监视
设备上的磁盘存储空间量.如果设备上的可用存储空间小于
可调阈值(安全设置参数;默认值为10%),则会显示低内存通知以提醒用户.如果用户单击低内存通知,则会启动Application Manager应用程序以使用户释放存储空间.
所以你有它.只要设备制造商不改变它,它将是10%.
稍微检查一下源代码,DeviceStorageMonitor发出一个粘性广播:(第354行)
mContext.sendStickyBroadcast(mStorageLowIntent);
Run Code Online (Sandbox Code Playgroud)
这意味着即使在广播结束后,您也可以通过在该意图上注册接收器来捕获数据.
来自Android Developer - Context:
执行"粘性"的sendBroadcast(Intent),意味着您发送的Intent在广播完成后保持不变,以便其他人可以通过registerReceiver(BroadcastReceiver,IntentFilter)的返回值快速检索该数据.在所有其他方面,这与sendBroadcast(Intent)的行为相同.
希望这可以帮助.
归档时间: |
|
查看次数: |
9895 次 |
最近记录: |