package cn.david.leaklesshandler;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.lang.ref.WeakReference;

/**
 * @author lyw
 * @date 2015-11-25
 */
public class MainActivity extends AppCompatActivity {

    private static final int MSG_OK = 888;
    MyHandler mHandler = new MyHandler(this);
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.main_text);

        // 开启线程
        new Thread(new DemoTask(this)).start();
    }

    /**
     * Handler 应使用静态内部类
     *
     * 将作为外部类的 Activity 作为 参数传进去，
     * 用弱引用持有其实例
     */
    private static class MyHandler extends Handler {
        private final WeakReference<MainActivity> activityReference;

        public MyHandler(MainActivity activity) {
            activityReference = new WeakReference<MainActivity>(
                    activity);
        }

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_OK:
                    final MainActivity activity = activityReference
                            .get();
                    if (activity != null && !activity.isFinishing()) {// 判断该页面是否还在， 在的话更新UI
                        activity.textView.setText("......");
                    }
                    break;

                default:
                    break;
            }
        }
    }
    /**
     * 继承自Runnable 的耗时操作也 应使用静态内部类
     *
     * 将作为外部类的 Activity 作为 参数传进去，
     * 用弱引用持有其实例
     */
    private static final class DemoTask implements Runnable {

        private final WeakReference<MainActivity> activityReference;

        public DemoTask( MainActivity activity ) {
            activityReference = new WeakReference<MainActivity>(
                    activity);
        }

        @Override
        public void run() {
            /****************************
             *
             *  ... 耗时操作
             *
             ****************************/


            final MainActivity activity = activityReference.get();
            if ( activity != null && !activity.isFinishing()) { // 判断该页面是否还在， 在的话发送消息
                activity.mHandler.obtainMessage(MSG_OK).sendToTarget();
            }
        }
    }
}

@@ -289,16 +289,95 @@
 e;%0A%0A/**%0A
+ *  %E9%9D%9E%E9%9D%99%E6%80%81%E5%86%85%E9%83%A8%E7%B1%BB%E4%BC%9A%E9%9A%90%E5%BC%8F%E7%9A%84%E6%8C%81%E6%9C%89%E5%A4%96%E9%83%A8%E7%B1%BB%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%8C%0A *  %E8%BF%99%E6%97%B6%E5%A6%82%E6%9E%9C%E5%86%85%E9%83%A8%E7%B1%BB%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%A4%A7%E4%BA%8E%E5%A4%96%E9%83%A8%E7%B1%BB%EF%BC%88Activity%EF%BC%89%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%0A *  %E5%B0%B1%E5%8F%AF%E8%83%BD%E4%BC%9A%E5%8F%91%E7%94%9F%E5%86%85%E5%AD%98%E6%B3%84%E9%9C%B2%0A *%0A
  * @auth
@@ -1609,16 +1609,20 @@
 ext(%22...
+data
 ...%22);%0A 

add annotation