step 1:定義一個監聽介面 step 2:android application文件中添加如下代碼 //初始化 //註冊監聽 //記憶體空間過低的時候,被系統調用 step 3:那些浪費記憶體的地方,比如圖片緩存,可以實現並註冊這個監聽 推薦:http://www.cnblogs.com/rouch ...
step 1:定義一個監聽介面
public static interface OnLowMemoryListener { void onLowMemoryReceived(); } /* 何問起 hovertree.com */
step 2:android application文件中添加如下代碼
//初始化
private ArrayList<WeakReference<OnLowMemoryListener>> mLowMemoryListeners; /* 何問起 hovertree.com */
public MyApp() { mLowMemoryListeners = new ArrayList<WeakReference<OnLowMemoryListener>>(); } /* 何問起 hovertree.com */
//註冊監聽
public void registerOnLowMemoryListener(OnLowMemoryListener listener) { if (listener != null) { mLowMemoryListeners .add(new WeakReference<MyApp.OnLowMemoryListener>(listener)); } } //移除監聽 public void unregisterOnLowMemoryListener(OnLowMemoryListener listener) { if (listener != null) { int i = 0; while (i < mLowMemoryListeners.size()) { final OnLowMemoryListener l = mLowMemoryListeners.get(i).get(); if (l == null || l == listener) { mLowMemoryListeners.remove(i); } else { i++; } } } } /* 何問起 hovertree.com */
//記憶體空間過低的時候,被系統調用
@Override public void onLowMemory() { super.onLowMemory(); int i = 0; while (i < mLowMemoryListeners.size()) { final OnLowMemoryListener l = mLowMemoryListeners.get(i).get(); if (l == null) { mLowMemoryListeners.remove(i); } else { l.onLowMemoryReceived(); i++; } } } /* 何問起 hovertree.com */
step 3:那些浪費記憶體的地方,比如圖片緩存,可以實現並註冊這個監聽
推薦:http://www.cnblogs.com/roucheng/p/androidtuichu.html