我正在构建一个应用程序来处理一些数据。该过程完成后,它会显示一条通知。如果我在完成进程后一段时间(同时执行一些繁重的任务)单击通知,它会崩溃。这是由于 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/