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;

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();
    }

    //
    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()) {
                        activity.textView.setText("......");
                    }
                    break;

                default:
                    break;
            }
        }
    }

    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();
            }
        }
    };
}

@@ -285,16 +285,59 @@
 rence;%0A%0A
+/**%0A * @author lyw%0A * @date 2015-11-25%0A */%0A
 public c
@@ -797,16 +797,107 @@
 %7D%0A%0A    /
+**%0A     * Handler %E5%BA%94%E4%BD%BF%E7%94%A8%E9%9D%99%E6%80%81%E5%86%85%E9%83%A8%E7%B1%BB%0A     *%0A     * %E5%B0%86%E4%BD%9C%E4%B8%BA%E5%A4%96%E9%83%A8%E7%B1%BB%E7%9A%84 Activity %E4%BD%9C%E4%B8%BA %E5%8F%82%E6%95%B0%E4%BC%A0%E8%BF%9B%E5%8E%BB%EF%BC%8C%0A     * %E7%94%A8%E5%BC%B1%E5%BC%95%E7%94%A8%E6%8C%81%E6%9C%89%E5%85%B6%E5%AE%9E%E4%BE%8B%0A     *
 /%0A    pr
@@ -1446,32 +1446,53 @@
 isFinishing()) %7B
+// %E5%88%A4%E6%96%AD%E8%AF%A5%E9%A1%B5%E9%9D%A2%E6%98%AF%E5%90%A6%E8%BF%98%E5%9C%A8%EF%BC%8C %E5%9C%A8%E7%9A%84%E8%AF%9D%E6%9B%B4%E6%96%B0UI
 %0A               
@@ -1665,24 +1665,132 @@
     %7D%0A    %7D%0A
+    /**%0A     * %E7%BB%A7%E6%89%BF%E8%87%AARunnable %E7%9A%84%E8%80%97%E6%97%B6%E6%93%8D%E4%BD%9C%E4%B9%9F %E5%BA%94%E4%BD%BF%E7%94%A8%E9%9D%99%E6%80%81%E5%86%85%E9%83%A8%E7%B1%BB%0A     *%0A     * %E5%B0%86%E4%BD%9C%E4%B8%BA%E5%A4%96%E9%83%A8%E7%B1%BB%E7%9A%84 Activity %E4%BD%9C%E4%B8%BA %E5%8F%82%E6%95%B0%E4%BC%A0%E8%BF%9B%E5%8E%BB%EF%BC%8C%0A     * %E7%94%A8%E5%BC%B1%E5%BC%95%E7%94%A8%E6%8C%81%E6%9C%89%E5%85%B6%E5%AE%9E%E4%BE%8B%0A     */
 %0A    private
@@ -2132,18 +2132,136 @@
    /
-/ ... %E8%80%97%E6%97%B6%E6%93%8D%E4%BD%9C
+****************************%0A             *%0A             *  ... %E8%80%97%E6%97%B6%E6%93%8D%E4%BD%9C%0A             *%0A             ****************************/%0A%0A
 %0A   
@@ -2387,16 +2387,38 @@
 ing()) %7B
+ // %E5%88%A4%E6%96%AD%E8%AF%A5%E9%A1%B5%E9%9D%A2%E6%98%AF%E5%90%A6%E8%BF%98%E5%9C%A8%EF%BC%8C %E5%9C%A8%E7%9A%84%E8%AF%9D%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF
 %0A       
@@ -2511,12 +2511,11 @@
  %7D%0A    %7D
-;
 %0A%7D%0A

add annotation