示例实现
public class AppContext extends Application {
//This my introduce OutOfMemoryException if you don't handle register and removal quiet well, better to replace it with weak reference
private static List<IMemoryInfo> memInfoList = new ArrayList<AppContext.IMemoryInfo>();
public static abstract interface IMemoryInfo {
public void goodTimeToReleaseMemory();
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
//don't compare with == as intermediate stages also can be reported, always better to check >= or <=
if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW) {
try {
// Activity at the front will get earliest than activity at the
// back
for (int i = memInfoList.size() - 1; i >= 0; i--) {
try {
memInfoList.get(i).goodTimeToReleaseMemory();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* @param implementor
* interested listening in memory events
*/
public static void registerMemoryListener(IMemoryInfo implementor) {
memInfoList.add(implementor);
}
public static void unregisterMemoryListener(IMemoryInfo implementor) {
memInfoList.remove(implementor);
}
}
public class ActivityParent extends Activity implements AppContext.IMemoryInfo {
protected ActivityParent child;
@Override
protected void onStop() {
super.onStop();
try {
if (child != null)
AppContext.unregisterMemoryListener(child);
} catch (Exception e) {
}
}
}
public class ActivityChild extends ActivityParent {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
child = this;
}
/---move following onResume() in parent as following eg:
/*
*@Override
* protected void onResume() {
* super.onResume();
* if(null != child){
* AppContext.registerMemoryListener(this);
* }
* }
*/
@Override
protected void onResume() {
super.onResume();
AppContext.registerMemoryListener(this);
}
@Override
public void goodTimeToReleaseMemory() {
super.goodTimeToReleaseMemory();
//remove your Cache etc here
}
//--NO Need because parent implementation will be called first, just for the sake of clarity
@Override
protected void onStop() {
super.onStop();
try {
if (null != child)
AppContext.unregisterMemoryListener(child);
} catch (Exception e) {
}
}
更多信息:
当您的应用程序正在运行时:
TRIM_MEMORY_RUNNING_MODERATE
设备开始内存不足。您的应用程序正在运行且不可终止。
TRIM_MEMORY_RUNNING_LOW
设备在内存上运行得更少。您的应用正在运行且不可终止,但请释放未使用的资源以提高系统性能(这会直接影响应用的性能)。
TRIM_MEMORY_RUNNING_CRITICAL
设备运行时内存极低。您的应用程序尚未被视为可终止进程,但如果应用程序不释放资源,系统将开始终止后台进程,因此您应该立即释放非关键资源以防止性能下降。
当您的应用的可见性发生变化时:
TRIM_MEMORY_UI_HIDDEN
您的应用的 UI 不再可见,因此这是释放仅由您的 UI 使用的大型资源的好时机。
当您的应用程序的进程驻留在后台 LRU 列表中时:
TRIM_MEMORY_BACKGROUND
系统内存不足,您的进程接近LRU
列表的开头。虽然你的应用进程被杀死的风险并不高,但系统可能已经在杀死LRU
列表中的进程,所以你应该释放易于恢复的资源,这样你的进程将保留在列表中,并在用户返回时快速恢复到您的应用程序。
TRIM_MEMORY_MODERATE
系统内存不足,您的进程接近 LRU 列表的中间位置。如果系统的内存进一步受限,则您的进程可能会被终止。
TRIM_MEMORY_COMPLETE
系统内存不足,如果系统现在不恢复内存,您的进程是最先被杀死的进程之一。您应该绝对释放对恢复应用程序状态不重要的所有内容。要支持低于 14 的 API 级别,您可以使用该onLowMemory()
方法作为大致相当于该TRIM_MEMORY_COMPLETE
级别的后备。
http://developer.android.com/reference/android/content/ComponentCallbacks2.html