当意图ACTION_DEVICE_STORAG​​E_LOW广播?意图、ACTION_DEVICE_STORAG、E_LOW

2023-09-05 03:09:19 作者:消失于温存ノ

在我的应用程序,我已经注册的广播接收器接收系统的意图ACTION_DEVICE_STORAG​​E_LOW。我原以为这是广播每当手机具有低内存。所以,我下载了一些额外的应用程序(我的手机有一个非常小的内存),这引起了操作系统,以显示该系统内存不足的通知。这款手机有10 - 15 MB的剩余。然而,我的广播接收器从来没有收到这一意图。然而,系统通知停留在通知栏,我无法浏览的,因为低内存互联网。

如果这种意图可以随时显示在低内存的通知广播?还是有一些内存甚至更低的阈值,将发出这个广播,我都打不还我的电话?在本文档中只说广播事件:一个持久广播,表示设备上的内存不足因为它实际上并没有界定内存不足的情况​​,我不知道如果我做错了什么,或者我根本没有打的情况呢。

下面是code我BroadcastReceiver的:

 公共类MemoryBroadcastReceiver扩展的BroadcastReceiver {

公共无效的onReceive(上下文的背景下,意图意图){

    串动= intent.getAction();

    如果(action.equals(Intent.ACTION_MEDIA_MOUNTED)){
        Log.isExternalStorageProblem = FALSE;
        Log.clearNotification(Log.externalMemoryNotificationID);
    }

    如果(action.equals(Intent.ACTION_DEVICE_STORAG​​E_OK)){
        Log.isInternalStorageLow = FALSE;
        Log.clearNotification(Log.internalMemoryNotificationID);
    }

    如果(action.equals(Intent.ACTION_DEVICE_STORAG​​E_LOW)){
        Log.isInternalStorageLow = TRUE;
        Log.displayMemoryNotification(内部存储低,
                内部存储容量很小,请解决这个问题。
                请清除高速缓存和/或卸载应用程序。,Log.internalMemoryNotificationID);
    }
}
}
 

我有一个初始化接收服务,增加了意图过滤器,并将其注册(除其他事项外):

 私人MemoryBroadcastReceiver memoryBroadcastReciever =新MemoryBroadcastReceiver();

公共无效registerBroadcastReceiver(){
    IntentFilter的过滤器=新的IntentFilter();
    filter.addAction(Intent.ACTION_DEVICE_STORAG​​E_OK);
    filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
    filter.addAction(Intent.ACTION_DEVICE_STORAG​​E_LOW);
    filter.addDataScheme(文件);

    。this.getApplicationContext()registerReceiver(memoryBroadcastReciever,过滤器);
}

@覆盖
公共无效的onCreate(){
    registerBroadcastReceiver();

  }
 

解决方案

TL; DR

只要设备制造商不会更改默认设置,其目的将在可用内存达到内部设备内存的10%播出。

龙版

我grepped通过Android源$ C ​​$下的意图,我得到了一类名为 DeviceStorageMonitorService 的

(位于:框架/基/服务/爪哇/ COM /安卓/服务器/ DeviceStorageMonitorService.java 的)

从javadoc的:

  

此类实现一个服务来监视磁盘量   在设备上的存储空间。如果设备的免费存储空间更少   比可调阈值(一个安全的设置参数;默认   10%)的低存储器通知显示,以提醒用户。如果   用户点击了低内存的通知应用程序   管理器应用程序被启动,让用户免费存储空间   空间。

所以你有它。只要设备制造商并没有改变的是,它会在10%以上。

签出源$ C ​​$ ca的有点多了,DeviceStorageMonitor散发出持久广播:(354线)

  mContext.sendStickyBroadcast(mStorageLowIntent);
 

这意味着广播结束,即使你可以通过注册上的意图接收器捕捉到的数据。

从Android开发者 - 上下文:

  

执行sendBroadcast(意向),也就是粘,意思是意向   您发送各地播出后完成的住宿,让   其他人可以通过迅速的返回值检索数据   registerReceiver(BroadcastReceiver的,IntentFilter的)。在所有其他方面,   这种行为一样sendBroadcast(意向)。

希望这会有所帮助。

In my application, I have registered a broadcast receiver to receive the system intent ACTION_DEVICE_STORAGE_LOW. I was expecting this to be broadcasted whenever the phone had low internal memory. So, I downloaded a few extra apps (my phone has a very small internal memory) which caused the OS to display the notification that the system memory is low. The phone had 10 - 15 MB remaining. However, my broadcast receiver never received that intent. Yet, the system notification stays in the notification bar, and I am not able to browse the internet because of low internal memory.

Should this intent be broadcasted whenever the low internal memory notification is displayed? Or is there some even lower threshold of memory that will send out this broadcast that I haven't hit yet on my phone? In the documentation it only says "Broadcast Action: A sticky broadcast that indicates low memory condition on the device." Since it doesn't actually define "low memory condition," I don't know if I am doing something wrong, or if I simply haven't hit that condition yet.

Here is the code for my 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);
    }
}
}

I have a service that initializes the receiver, adds the intent filter and registers it (among other things):

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

As long as the device maker doesn't change the default settings, the intent will be broadcast when the free memory reaches 10% of the internal device memory.

Long Version

I grepped through the Android source code for that intent and I got to a class called DeviceStorageMonitorService

(located at: frameworks/base/services/java/com/android/server/DeviceStorageMonitorService.java)

From the 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.

So there you have it. As long as the device maker doesn't change that, it will be at 10%.

Checking out the source code a bit more, the DeviceStorageMonitor sends out a sticky Broadcast: (line 354)

mContext.sendStickyBroadcast(mStorageLowIntent);

Which means that even after the broadcast is finished you can catch the data by registering a receiver on that intent.

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

hope this helps.