Android Handler使用

Android Handler使用

说明:Handler的使用与消息密切相关,知道消息的系统结构及发送过程就能很好的使用handler为我们服务。

消息系统构成:

  • 消息原型
  • 消息队列
  • 消息生产
  • 消息循环
  • 消息消费

注:Message就是消息原型,包含消息描述和数据,
  MessageQueue就是消息队列,
  Looper完成消息循环,
  Handler就是驾驭整个消息系统模型,包括消息生产和消费,统领Message,MessgeQueue和Looper;

消息工作图:

Created with Raphaël 2.1.0 Handler Handler MessageQueqe MessageQueqe Looper Looper Send/Post Message(发送消息) 循环获取消息,判断是否有消息, 有消息唤醒,没有消息则挂起) DispatchMessage(分发消息) HandlerMessage(处理消息)

测试线程间消息发送:

注:测试子线程到主线程、主线程到子线程及子线程到子线程三种情况下之间的消息传递,例子如下:

package com.xyy.collection.test;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.xyy.collection.utils.LogUtils;

/** * @Description: 测试线程间发送消息 * @author: <a href="http://xiaoyaoyou1212.360doc.com">DAWI</a> * @date: 2015-11-12 16:00 */
public class TestMessageSend {

    private static TestMessageSend testMessageSend;
    private Handler handlerA;
    private Handler handler;
    private ThreadA threadA;
    private ThreadB threadB;

    private static final int CHILD_TO_MAIN = 0;//子线程发消息到主线程
    private static final int MAIN_TO_CHILD = 1;//主线程发消息到子线程
    private static final int CHILD_TO_CHILD = 2;//子线程发消息到子线程

    public static TestMessageSend getInstance(){
        if(testMessageSend == null){
            synchronized (TestMessageSend.class){
                if(testMessageSend == null){
                    testMessageSend = new TestMessageSend();
                }
            }
        }
        return testMessageSend;
    }

    public void init(){
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                LogUtils.e("ThreadMain:" + Thread.currentThread().getName());
                LogUtils.e("messageMain"+msg.what);
            }
        };
        threadA = new ThreadA();
        threadA.start();
    }

    public void sendMessageInThread(){
        threadB = new ThreadB();
        threadB.start();
        if(handlerA != null){
            handlerA.sendEmptyMessage(MAIN_TO_CHILD);
        }
    }

    private class ThreadA extends Thread{
        @Override
        public void run() {
            Looper.prepare();
            handlerA = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    LogUtils.e("ThreadA:" + Thread.currentThread().getName());
                    LogUtils.e("messageA"+msg.what);
                }
            };
            Looper.loop();
        }
    }

    private class ThreadB extends Thread{
        @Override
        public void run() {
            LogUtils.e("ThreadB:" + Thread.currentThread().getName());
            if(handlerA != null){
                handlerA.sendEmptyMessage(CHILD_TO_CHILD);
            }
            if(handler != null){
                handler.sendEmptyMessage(CHILD_TO_MAIN);
            }
        }
    }
}
  • 在Activity中先使用TestMessageSend.getInstance().init();进行初始化,再调用TestMessageSend.getInstance().sendMessageInThread();运行结果如下:
11-15 14:52:59.490  27213-27372/com.xyy.collection E/TestMessageSend$ThreadB.run(L:75)﹕ ThreadB:Thread-2618
11-15 14:52:59.490  27213-27234/com.xyy.collection E/TestMessageSend$ThreadA$1.handleMessage(L:64)﹕ ThreadA:Thread-2617
11-15 14:52:59.500  27213-27234/com.xyy.collection E/TestMessageSend$ThreadA$1.handleMessage(L:65)﹕ messageA2
11-15 14:52:59.500  27213-27234/com.xyy.collection E/TestMessageSend$ThreadA$1.handleMessage(L:64)﹕ ThreadA:Thread-2617
11-15 14:52:59.500  27213-27234/com.xyy.collection E/TestMessageSend$ThreadA$1.handleMessage(L:65)﹕ messageA1
11-15 14:52:59.500  27213-27213/com.xyy.collection E/TestMessageSend$1.handleMessage(L:41)﹕ ThreadMain:main
11-15 14:52:59.500  27213-27213/com.xyy.collection E/TestMessageSend$1.handleMessage(L:42)﹕ messageMain0

你可能感兴趣的:(android,handler,message,消息,looper)