|
| 1 | +# Android 防内存泄露handler |
| 2 | + |
| 3 | + |
| 4 | +# 1.使用弱引用 WeakRefHander |
| 5 | +```java |
| 6 | + /** |
| 7 | + * 作者: allen on 15/11/24.感谢开源作者https://coding.net/u/coding/p/Coding-Android/git |
| 8 | + */ |
| 9 | + |
| 10 | + /** |
| 11 | + * 弱引用 handler 防止内存泄露 |
| 12 | + */ |
| 13 | + public class WeakRefHander extends Handler { |
| 14 | + |
| 15 | + private final WeakReference<Handler.Callback> mRef; |
| 16 | + private final int mLoopTime; |
| 17 | + private int NO_LOOP = -1; |
| 18 | + private int what =0; |
| 19 | + |
| 20 | + /** |
| 21 | + * 循环 |
| 22 | + * |
| 23 | + * @param loopAction |
| 24 | + * @param loopTime |
| 25 | + */ |
| 26 | + public WeakRefHander(Handler.Callback loopAction, int loopTime) { |
| 27 | + super(); |
| 28 | + this.mRef = new WeakReference<>(loopAction); |
| 29 | + this.mLoopTime = loopTime; |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * 不循环 |
| 35 | + * |
| 36 | + * @param loopAction |
| 37 | + */ |
| 38 | + public WeakRefHander(Handler.Callback loopAction) { |
| 39 | + super(); |
| 40 | + mRef = new WeakReference<>(loopAction); |
| 41 | + mLoopTime = NO_LOOP; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void handleMessage(Message msg) { |
| 46 | + Handler.Callback action = mRef.get(); |
| 47 | + if (action != null) { |
| 48 | + action.handleMessage(msg); |
| 49 | + if (mLoopTime != NO_LOOP) { |
| 50 | + sendEmptyMessageDelayed(what, mLoopTime); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public void start() { |
| 56 | + removeMessages(0); |
| 57 | + sendEmptyMessageDelayed(0, 0); |
| 58 | + } |
| 59 | + |
| 60 | + public void start(int what, long delay) { |
| 61 | + this.what = what; |
| 62 | + removeMessages(what); |
| 63 | + sendEmptyMessageDelayed(what, delay); |
| 64 | + } |
| 65 | + |
| 66 | + public void stop() { |
| 67 | + removeMessages(what); |
| 68 | + } |
| 69 | + |
| 70 | + public void clear() { |
| 71 | + removeMessages(what); |
| 72 | + mRef.clear(); |
| 73 | + } |
| 74 | + } |
| 75 | +``` |
| 76 | + |
| 77 | +## 2. 实现 Activity implements WeakRefHander.Callback |
| 78 | + |
| 79 | +## 3. 在handleMessage处理业务逻辑 |
| 80 | + |
| 81 | +# 示例代码: |
| 82 | + |
| 83 | +```java |
| 84 | + public class MainActivity extends AppCompatActivity implements WeakRefHander.Callback { |
| 85 | + private WeakRefHander weakRefHander; |
| 86 | + private static final int HANDLER_MESSAGE_START = 1001; |
| 87 | + @Override |
| 88 | + protected void onCreate(Bundle savedInstanceState) { |
| 89 | + super.onCreate(savedInstanceState); |
| 90 | + setContentView(R.layout.activity_main); |
| 91 | + weakRefHander = new WeakRefHander(this, 1); |
| 92 | + weakRefHander.start(HANDLER_MESSAGE_START, 1000 * 30); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean handleMessage(Message msg) { |
| 97 | + switch (msg.what) { |
| 98 | + case HANDLER_MESSAGE_START: |
| 99 | + //Todo 处理业务逻辑 |
| 100 | + break; |
| 101 | + default: |
| 102 | + break; |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + @Override |
| 109 | + public void onResume() { |
| 110 | + super.onResume(); |
| 111 | + weakRefHander.start(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void onPause() { |
| 116 | + weakRefHander.stop(); |
| 117 | + super.onPause(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + protected void onDestroy() { |
| 122 | + weakRefHander.clear(); |
| 123 | + super.onDestroy(); |
| 124 | + } |
| 125 | + } |
| 126 | +``` |
| 127 | + |
| 128 | +# -[Github示例参考代码](https://github.com/AllenCoder/AndroidDevCoder/tree/master/leakhandler) |
| 129 | + |
| 130 | +# 参考作者:-[Coding-Android作者](https://coding.net/u/coding/p/Coding-Android/git) |
0 commit comments