android - 当系统杀死我的应用程序以释放内存时如何得到通知?

标签 android android-activity memory-management

我正在构建一个应用程序来处理一些数据。该过程完成后,它会显示一条通知。如果我在完成进程后一段时间(同时执行一些繁重的任务)单击通知,它会崩溃。这是由于 android 清理了应用程序使用的内存。当android强制关闭应用程序时,是否有任何回调方法/方式来获得通知?

最佳答案

public void onLowMemory ()

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage. While the exact point at which this will be called is not defined, generally it will happen when all background process have been killed. That is, before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing.

You should implement this method to release any caches or other unnecessary resources you may be holding on to. The system will perform a garbage collection for you after returning from this method.

Preferably, you should implement ComponentCallbacks2.onTrimMemory(int) from ComponentCallbacks2 to incrementally unload your resources based on various levels of memory demands. That API is available for API level 14 and higher, so you should only use this onLowMemory() method as a fallback for older versions, which can be treated the same as ComponentCallbacks2.onTrimMemory(int) with the ComponentCallbacks2.TRIM_MEMORY_COMPLETE level.

这里是文档:

https://developer.android.com/reference/android/app/Activity.html#onLowMemory()

关于android - 当系统杀死我的应用程序以释放内存时如何得到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50814467/

相关文章:

ios - 导航回父场景时解除 segue 问题

PhpUnit 与 Zend Framework 内存问题

java - 在已安装的应用程序上回调 google play

java - Android 未找到 Activity - STILL_IMAGE_CAMERA

android - 从 JSON 包中检索信息

java - 从另一个 Activity Android studio 导入数据

android - 为什么要在服务中使用 startActivity

java - 如何使用 Parcelable 发送多维数组?

java - adb连接错误:EOF (new in android )

javascript - JavaScript Array 的内存管理