BaseHandler的封装, 处理handler中的内存泄漏

Posted MarcoReus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BaseHandler的封装, 处理handler中的内存泄漏相关的知识,希望对你有一定的参考价值。

package de.bvb.study.common;

/**
 * 用于规范 Message.what此属性,避免出现魔法数字
 */
public final class What {

    public static final int ZERO = 0;
    public static final int ONE = 1;

    /** 标记异步操作返回时目标界面已经消失时的处理状态 */
    public static final int ACTIVITY_GONE = -1;
}
package de.bvb.study.common;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;

import java.lang.ref.WeakReference;

import de.bvb.study.util.LogUtil;

/**
 * handler包装类
 */
public abstract class BaseHandler extends Handler {

    protected WeakReference<Activity> activityWeakReference;
    protected WeakReference<Fragment> fragmentWeakReference;

    private BaseHandler() {}//构造私有化,让调用者必须传递一个Activity 或者 Fragment的实例

    public BaseHandler(Activity activity) {
        this.activityWeakReference = new WeakReference<>(activity);
    }

    public BaseHandler(Fragment fragment) {
        this.fragmentWeakReference = new WeakReference<>(fragment);
    }

    @Override
    public void handleMessage(Message msg) {
        if (activityWeakReference == null || activityWeakReference.get() == null || activityWeakReference.get().isFinishing()) {
            // 确认Activity是否不可用
            LogUtil.i("Activity is gone");
            handleMessage(msg, What.ACTIVITY_GONE);
        } else if (fragmentWeakReference == null || fragmentWeakReference.get() == null || fragmentWeakReference.get().isRemoving()) {
            //确认判断Fragment不可用
            LogUtil.i("Fragment is gone");
            handleMessage(msg, What.ACTIVITY_GONE);
        } else {
            handleMessage(msg, msg.what);
        }
    }

    /**
     * 抽象方法用户实现,用来处理具体的业务逻辑
     *
     * @param msg
     * @param what {@link What}
     */
    public abstract void handleMessage(Message msg, int what);

}
package de.bvb.study.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;

import de.bvb.study.common.BaseHandler;
import de.bvb.study.common.What;

public class LoginActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.obtainMessage(What.ZERO, "data 0").sendToTarget();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.obtainMessage(What.ONE, "data 1").sendToTarget();
            }
        }).start();

    }

    MyHandler handler = new MyHandler(this);
    private static class MyHandler extends BaseHandler {

        public MyHandler(Activity activity) {
            super(activity);
        }

        @Override
        public void handleMessage(Message msg, int what) {
            switch (what) {
                case What.ZERO:
                    break;
                case What.ONE:
                    break;
                case What.ACTIVITY_GONE:
                    break;
            }
        }
    }
}

 

以上是关于BaseHandler的封装, 处理handler中的内存泄漏的主要内容,如果未能解决你的问题,请参考以下文章

javaScript跨浏览器事件处理封装代码

消息处理Handler

Android---Handler消息处理机制

深入源码分析Handler的消息处理机制

Android中的多线程编程附源代码

Handler类和Handler,Loop,MessageQueue的工作原理