zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }
    }
  • 相关阅读:
    [Wap] 制作自定义WmlListAdapter来实现Mobile.List控件的各种效果
    [EntLib]UAB(Updater Application Block)下载
    jarhoo是一个很棒的地方
    [GoogleMap]利用GoogleMap地图的这个应用真是太狠了[1]
    [J2ME] VideoCoolala(MobileWebCam)开源说明
    [p2p]手机是否可以通过JXTA网络与PC机/PocketPC/WindowsMobile实现P2P呢?
    Android Layout XML属性
    什么是9.png
    android主流UI布局
    Android开发之旅: Intents和Intent Filters(理论部分)
  • 原文地址:https://www.cnblogs.com/Westfalen/p/6287479.html
Copyright © 2011-2022 走看看