android – 广播ACTION_DEVICE_STORAGE_LOW的意图是什么时候?

前端之家收集整理的这篇文章主要介绍了android – 广播ACTION_DEVICE_STORAGE_LOW的意图是什么时候?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,我已经注册了一个广播接收器来接收系统意图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();

  }

解决方法

TL; DR

只要设备制造商不更改默认设置,当可用内存达到内部设备内存的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);

这意味着即使在广播完成后,您可以通过注册接收者来捕获数据.

Android Developer – Context

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).

希望这可以帮助.

原文链接:https://www.f2er.com/android/313656.html

猜你在找的Android相关文章