每当显示低内部存储器通知时,是否应广播这个意图?还是有一些甚至更低的内存阈值,会发出这个广播,我还没有打到我的手机?在文档中,它只说“广播操作:指示设备内存条件低的粘性广播”.既然它没有实际定义“低内存条件”,我不知道我做错了什么,或者我根本没有达到这个条件.
以下是我的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(); }
解决方法
只要设备制造商不更改默认设置,当可用内存达到内部设备内存的10%时,意图将被广播.
长版
我通过这个意图的Android源代码,我得到了一个名为DeviceStorageMonitorService的类
(位于:frameworks / base / services / java / com / android / server / DeviceStorageMonitorService.java)
从javadoc:
This class implements a service to monitor the amount of disk
storage space on the device. If the free storage on device is less
than a tunable threshold value (a secure settings parameter; default
10%) a low memory notification is displayed to alert the user. If
the user clicks on the low memory notification the Application
Manager application gets launched to let the user free storage
space.
所以你有它.只要设备制造商没有改变,它将在10%.
检查源代码,DeviceStorageMonitor发出一个粘性广播:(第354行)
mContext.sendStickyBroadcast(mStorageLowIntent);
这意味着即使在广播完成后,您可以通过注册接收者来捕获数据.
Perform a sendBroadcast(Intent) that is “sticky,” meaning the Intent
you are sending stays around after the broadcast is complete,so that
others can quickly retrieve that data through the return value of
registerReceiver(BroadcastReceiver,IntentFilter). In all other ways,
this behaves the same as sendBroadcast(Intent).
希望这可以帮助.