Message and
Runnable objects associated with a thread‘s MessageQueue.
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.post(Runnable), postAtTime(Runnable,
long), postDelayed(Runnable,
long), sendEmptyMessage(int), sendMessage(Message),sendMessageAtTime(Message,
long), and sendMessageDelayed(Message,
long) methods. Message object
containing a bundle of data that will be processed by the Handler‘shandleMessage(Message) method
(requiring that you implement a subclass of Handler).handleMessage(Message) 这个函数来获取其值。
public class NasaDaliyImage extends Activity {
public IotdHandler handler;
private ProgressDialog dialog;
private Handler handlerRun ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nasa_daliy_image);
handlerRun =new Handler();//将Handler关联至UI线程
refreshFromFeed();
}
private void refreshFromFeed() {
// doSomeThing 可执行UI操作
Thread th = new Thread() { //新建线程
public void run() {
//执行耗时的网络操作等
handlerRun.post( // post Runnable对象
new Runnable () { //实现Runnable接口
public void run() { //重写Runnable的run()方法
//执行UI操作
}
}
);
}
};
th.start(); //线程启动
}
class MyHandler extends Handler { public MyHandler() { } public MyHandler(Looper L) { super(L); //Use the provided Looper instead of the default one. } // 重写handleMessage方法,接受数据并更新UI @Override public void handleMessage(Message msg) { super.handleMessage(msg); //此处根据msg内容进行UI操作 } } // 在UI线程中执行 MyHandler handler = new MyHandler();不带参数将默认管理UI线程。
class MyThread implements Runnable { public void run() { //执行费时的操作 Message msg = new Message(); Bundle b = new Bundle(); b.putString("cmd", "update"); msg.setData(b); MainActivity.this.myHandler.sendMessage(msg);//通知Handler更新UI MainActivity为其主线程 } }
Looper,Class used to run a message loop for a thread.维护线程的消息循环类。
Handler,A Handler allows you to send and process Message and Runnable objects associated with a thread‘s MessageQueue。向消息队列发送消息(Runnable object),或者执行消息队列的消息(Runnable object).
下面介绍下Looper
public class LooperThread extends Thread { private Handler handler1; private Handler handler2; @Override public void run() { // 将当前线程初始化为Looper线程 Looper.prepare(); // 实例化两个handler handler1 = new Handler(); handler2 = new Handler(); // 开始循环处理消息队列 Looper.loop(); } }
ActivityThread.java public static final void main(String[] args) { Looper.prepareMainLooper(); ActivityThread thread = new ActivityThread(); thread.attach(false); // 这里闭合消息循环 Looper.loop(); }
其中Looper.loop();是looper不断的读取队列中的消息
再次查看位于\sdk\sources\android-16\android\os\Handler.java中的dispatchMessage函数,分别执行Runnable或者调用handleMessage进行处理。public static final void loop() { Looper me = myLooper(); //得到当前线程Looper MessageQueue queue = me.mQueue; //得到当前looper的MQ // 这两行没看懂= = 不过不影响理解 Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity(); // 开始循环 while (true) { Message msg = queue.next(); // 取出message if (msg != null) { if (msg.target == null) { // message没有target为结束信号,退出循环 return; } // 日志。。。 if (me.mLogging!= null) me.mLogging.println( ">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what ); // 非常重要!将真正的处理工作交给message的target,即后面要讲的handler msg.target.dispatchMessage(msg); // 还是日志。。。 if (me.mLogging!= null) me.mLogging.println( "<<<<< Finished to " + msg.target + " " + msg.callback); // 下面没看懂,同样不影响理解 final long newIdent = Binder.clearCallingIdentity(); if (ident != newIdent) { Log.wtf("Looper", "Thread identity changed from 0x" + Long.toHexString(ident) + " to 0x" + Long.toHexString(newIdent) + " while dispatching to " + msg.target.getClass().getName() + " " + msg.callback + " what=" + msg.what); } // 回收message资源 msg.recycle(); } } }
// 处理消息,该方法由looper调用 public void dispatchMessage(Message msg) { if (msg.callback != null) { // 如果message设置了callback,即runnable消息,处理callback! handleCallback(msg); } else { // 如果handler本身设置了callback,则执行callback if (mCallback != null) { /* 这种方法允许让activity等来实现Handler.Callback接口,避免了自己编写handler重写handleMessage方法。 if (mCallback.handleMessage(msg)) { return; } } // 如果message没有callback,则调用handler的钩子方法handleMessage handleMessage(msg); } } // 处理runnable消息 private final void handleCallback(Message message) { message.callback.run(); //直接调用run方法! } // 由子类实现的钩子方法 public void handleMessage(Message msg) { }
原文:http://blog.csdn.net/wolinxuebin/article/details/42984111