buder

20171126-handler消息机制理解

1.handler消息机制的理解

http://www.jianshu.com/p/8343a39b8a2c?s_q_s_h_a_r_e_1MTAzNTIwODAxNTExNTg5NTkwMzE0NzkwMzAxJjI5czczVA==

 

创建不会内存泄露的handler标准写法:

public class SampleActivity extends Activity {

    private static class MyHandler extends Handler {

        private WeakReference<SampleActivity> activityWeakReference;

        private MyHandler(SampleActivity activity) {
            activityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            SampleActivity sampleActivity = activityWeakReference.get();
            if (sampleActivity != null) {
                sampleActivity.handleMessage(msg);
            }
        }
    }

    private MyHandler mHandler = new MyHandler(SampleActivity.this);

    private void handlerMessage(Message msg) {
        // 处理消息
    }
}

2.handler.sendMessage()以及handler.post()的区别以及理解:

http://blog.csdn.net/fel835/article/details/52004392

http://blog.csdn.net/u012534831/article/details/52212239

handler.sendMessage的写法:

final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 1) {
                    //doSomeThing
                }
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        });

 

handler.post的写法:

final Handler handler = new Handler();
new Thread(new Runnable() {
            @Override
            public void run() {
                final String response = get(url);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //doSomeThing
                    }
                });
            }
        }).start();

 

3.在子线程中创建handler的写法:

方法一:(直接获取当前子线程的looper)如果我们想在子线程上创建Handler,通过直接new的出来是会报异常,(因为正常主线程中handler会自动创建loop)

 

new Thread(new Runnable() {  
            public void run() {  
                Looper.prepare();  
                Handler handler = new Handler(){  
                    @Override  
                    public void handleMessage(Message msg) {  
                        Toast.makeText(getApplicationContext(), "handler msg", Toast.LENGTH_LONG).show();  
                    }  
                };  
                handler.sendEmptyMessage(1);  
                Looper.loop();  
            };  
        }).start();  

 

 

 

方法二:(获取主线程的looper,或者说是UI线程的looper)

 

new Thread(new Runnable() {  
            public void run() {  
                Handler handler = new Handler(Looper.getMainLooper()){ // 区别在这!!!!  
                    @Override  
                    public void handleMessage(Message msg) {  
                        Toast.makeText(getApplicationContext(), "handler msg", Toast.LENGTH_LONG).show();  
                    }  
                };  
                handler.sendEmptyMessage(1);  
            };  
        }).start();  

 

 

 

4.

 

5.

 

6.

 

7.

 

8.

 

9.

posted on 2017-11-26 14:12  buder  阅读(171)  评论(0编辑  收藏  举报

导航