Chinaunix首页 | 论坛 | 博客
  • 博客访问: 258203
  • 博文数量: 164
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1129
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 21:55
文章分类

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Android平台

2015-04-04 18:54:28

 handler 发送处理步骤:
    


    //获取消息对象
    Message msg = new Message();
    //效率高(如果消息池中有消息对象,如果没有再去创建)
    Message msg = Message.obtain();

    Handler handler = new Handler(){
        handlerMessage(Message msg){
            //处理消息(更新UI)
        }
    }

    handler.sendMessage(msg)

-------------------------------------------------------------------------

    //获取消息对象,核心实现
    public static Message obtain() {
        synchronized (mPoolSync) {
            if (mPool != null) {
                Message m = mPool;
                mPool = m.next;
                m.next = null;
                return m;
            }
        }
        return new Message();
    }

    //Handler对应构造方法源码
        public Handler() {
        ...
        mLooper = Looper.myLooper();
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;
        mCallback = null;
    }

    //Looper.myLooper();调用当前方法
    public static final Looper myLooper() {
        return (Looper)sThreadLocal.get();
    }
    
    
    public static final void prepareMainLooper() {
        prepare();
        setMainLooper(myLooper());
        if (Process.supportsProcesses()) {
            myLooper().mQueue.mQuitAllowed = false;
        }
    }

    public static final void prepare() {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper());
    }
    
    //根据prepareMainLooper断定当前方法在主线程中调用ActivityThread优先去观看main方法
    
    
    public static final void main(String[] args) {
           ...
        Looper.prepareMainLooper();
           ...
        Looper.loop();
        ...
    }

    //Looper.loop方法
    public static final void loop() {
        Looper me = myLooper();
        //获取消息队列
        MessageQueue queue = me.mQueue;
        while (true) {
            //在循环中获取消息队列的第一个消息
            Message msg = queue.next(); // might block(睡眠)
            ...
            msg.target.dispatchMessage(msg);
            ....
        }
    }

Handler机制主要采用 linux下管道设计(Pipe)进程间通信,生产者消费者
文件,写入的描述符,读取的描述符

写入的描述符:子线程里面发送消息,获取写入的描述符,向指定的文件写入一个内容,sendMessage()方法,将消息添加至消息队列,sendMessage()方法会去唤醒睡眠的主线程,。

读取的描述符:主线程中持有,读取文件,一旦文件中有内容,消息队列里面有消息,取消息并且进行处理。如果没有取到消息,睡眠,当接受到sendMessage()方法发出的标示后,继续去取消息。



     public void dispatchMessage(Message msg) {
           ...
        //调用Handler中自己实现的handlerMessage(msg);
        handleMessage(msg);
    }


------------------------------添加消息至消息列-------------------------------
    //Handler中sendMessage方法
    public final boolean sendMessage(Message msg)
    {
        return sendMessageDelayed(msg, 0);
    }

     public final boolean sendMessageDelayed(Message msg, long delayMillis)
    {
        ...
        //手机开启时间+延时处理消息时间(时间戳)
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }
    //转调sendMessageAtTime方法
    public boolean sendMessageAtTime(Message msg, long uptimeMillis)
    {
        ...
        MessageQueue queue = mQueue;
        if (queue != null) {
            //msg.target即为handler对象
            msg.target = this;
    
            sent = queue.enqueueMessage(msg, uptimeMillis);
        }
        ...
    }

    MessageQueue中enqueueMessage方法

    
    final boolean enqueueMessage(Message msg, long when) {
            ...
            msg.when = when;
            //Log.d("MessageQueue", "Enqueing: " + msg);
            Message p = mMessages;
            if (p == null || when == 0 || when < p.when) {
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked; // new head, might need to wake up
            } else {
                Message prev = null;
                while (p != null && p.when <= when) {
                    prev = p;
                    p = p.next;
                }
                msg.next = prev.next;
                prev.next = msg;
                ...
            }
        }
       ...
    }
阅读(757) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~