展会信息港展会大全

handler、message、looper之间关系的学习 android开发
来源:互联网   发布日期:2016-01-19 12:32:41   浏览:4394次  

导读:messagemessage是携带消息的,其中属性有如:int arg01,arg02;Object obj;int what;等等,它的 声明方式最好使用Message obtain() 这是为什么呢?请看下面它的源码 这是一部分android源码,可以自己查看完 ...

message

message是携带消息的,其中属性有如:int arg01,arg02;Object obj;int what;等等,它的 声明方式最好使用Message.obtain() 这是为什么呢?请看下面它的源码

//这是一部分android源码,可以自己查看完整的

public static Message obtain() {

synchronized (sPoolSync) {

if (sPool != null) {

Message m = sPool;

sPool = m.next;

m.next = null;

sPoolSize--;

return m;

}

}

return new Message();

}

Message.obtain() 这个方法是从消息池中取出消息对象,如果没有才创建,所以减少了资源开销

handler

handler是处理消息即发送和接收,你可能会想这不是逗我吗?发送也是他接收也是他,其实是有缘由的。

首先handler在声明的时候会和声明他的线程的消息队列绑定官方API如下:

Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

当他绑定后,发送消息就可以在子线程中发送到消息队列,然后在主线程中接收,从而达到了线程通信的功能,可能现在有点迷惑,别急看了looper后再回来理解原因。

looper

looper的功能创建消息队列(MessageQueue),并管理维护handler对消息队列的操作。如果你昏了,说明在理解的道路上了,没昏说明已经理解了一部分。

理一下思路

最基本的使用步骤

创建Looper对象获得消息队列,创建handler绑定消息队列,子线程中利用handler.sendMessage(Message msg)发送消息,主线程中handler的handleMessage(Message msg)消息的处理

当我们在Acivity中创建handler的时候可以直接这么写:

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_edite_text);

handler = new Handler() {

//处理接收到的消息

public void handleMessage(Message msg) {

System.out.println("--msg.arg1-->" + msg.arg1);

};

};

new Thread(new MyThread()).start();

}

class MyThread implements Runnable {

@Override

public void run() {

Message msg = Message.obtain();

msg.arg1 = 1;

// 发送消息到绑定的消息队列中。

handler.sendMessage(msg);

}

}

}

问题来了,我没声明looper啊哪里来的消息队列呢?这是因为handler是在activity 的线程中被创建的,handler与activity的默认的looper对象绑定了。所以消息可以从子线程中发送到主线程中实现UI的更新。

可以再细想一下为什么handleMessage就可以收到消息呢?什么时候被执行的呢?这个就可以通过观看 handler.sendMessage()和looper的源码解决,sendMessage是把消息加入到绑定的消息队列中,然后looper对象去循环把消息队列中的message发送到handlerMessage()中,即looper内部调用了handleMessage()方法,可以看 looper的源代码中发现。

为了验证我们可以,查看官方的API的looper例子:

class LooperThread extends Thread {

public Handler mHandler;

public void run() {

Looper.prepare();

mHandler = new Handler() {

public void handleMessage(Message msg) {

// process incoming messages here

}

};

Looper.loop();

}

}

为什么有Looper.prepare();Looper.loop();这两个方法的出现呢,官方的 API也指出线程(thread)原本是没有looper即消息队列的要是用Looper.prepare()来创建,Looper.loop()来调用 handler的handleMessage()处理消息,通过看两个方法的源代码更能够看出来缘由。

赞助本站

人工智能实验室

相关热词: handler message looper android

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港