Android系统的消息队列和消息循环都是针对具体线程的,一个线程能够存在(固然也能够不存在)一个消息队列(Message Queue)和一个消息循环(Looper)。Android中除了UI线程(主线程),建立的工做线程默认是没有消息循环和消息队列的。若是想让该线程 具备消息队列和消息循环,并具备消息处理机制,就须要在线程中首先调用Looper.prepare()来建立消息队列,而后调用 Looper.loop()进入消息循环。如如下代码所示:java
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()来建立消息队列,会 报"java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()"的错误。android
经过下图能够清晰显示出UI Thread, Worker Thread, Handler, Massage Queue, Looper之间的关系:安全
解释上图中的几个基本概念:数据结构
1.Messageapp
消息对象,顾名思义就是记录消息信息的类。这个类有几个比较重要的字段:ide
a.arg1和arg2:咱们可使用两个字段用来存放咱们须要传递的整型值,在Service中,咱们能够用来存放Service的ID。oop
b.obj:该字段是Object类型,咱们可让该字段传递某个多项到消息的接受者中。post
c.what:这个字段能够说是消息的标志,在消息处理中,咱们能够根据这个字段的不一样的值进行不一样的处理,相似于咱们在处理Button事件时,经过switch(v.getId())判断是点击了哪一个按钮。spa
在使用Message时,咱们能够经过new Message()建立一个Message实例,可是Android更推荐咱们经过Message.obtain()或者 Handler.obtainMessage()获取Message对象。这并不必定是直接建立一个新的实例,而是先从消息池中看有没有可用的 Message实例,存在则直接取出并返回这个实例。反之若是消息池中没有可用的Message实例,则根据给定的参数new一个新Message对象。 经过分析源码可得知,Android系统默认状况下在消息池中实例化10个Message对象。线程
2.MessageQueue
消息队列,用来存放Message对象的数据结构,按照“先进先出”的原则存放消息。存放并不是实际意义的保存,而是将Message对象以链表的方式串联 起来的。MessageQueue对象不须要咱们本身建立,而是有Looper对象对其进行管理,一个线程最多只能够拥有一个MessageQueue。 咱们能够经过Looper.myQueue()获取当前线程中的MessageQueue。
3.Looper
MessageQueue的管理者,在一个线程中,若是存在Looper对象,则一定存在MessageQueue对象,而且只存在一个Looper对象 和一个MessageQueue对象。假若咱们的线程中存在Looper对象,则咱们能够经过Looper.myLooper()获取,此外咱们还能够通 过Looper.getMainLooper()获取当前应用系统中主线程的Looper对象。在这个地方有一点须要注意,假如Looper对象位于应用 程序主线程中,则Looper.myLooper()和Looper.getMainLooper()获取的是同一个对象。
4.Handler
消息的处理者。经过Handler对象咱们能够封装Message对象,而后经过sendMessage(msg)把Message对象添加到 MessageQueue中;当MessageQueue循环到该Message时,就会调用该Message对象对应的handler对象的 handleMessage()方法对其进行处理。因为是在handleMessage()方法中处理消息,所以咱们应该编写一个类继承自 Handler,而后在handleMessage()处理咱们须要的操做。
另外,咱们知道,Android UI操做并非线程安全的,因此没法在子线程中更新UI。但Andriod提供了几种方法,能够在子线程中通知UI线程更新界面:
Activity.runOnUiThread( Runnable )
View.post( Runnable )
View.postDelayed( Runnable, long )
Handler
比较经常使用的是经过Handler,用Handler来接收子线程发送的数据,并用此数据配合主线程更新UI。那么,只要在主线程中建立Handler对 象,在子线程中调用Handler的sendMessage方法,就会把消息放入主线程的消息队列,而且将会在Handler主线程中调用该 handler的handleMessage方法来处理消息。
package com.superonion; import android.app.Activity; import android.os.Bundle; import android.os.Message; import android.util.Log; import android.os.Handler; public class MyHandler extends Activity { static final String TAG = "Handler"; Handler h = new Handler(){ public void handleMessage (Message msg) { switch(msg.what) { case HANDLER_TEST: Log.d(TAG, "The handler thread id = " + Thread.currentThread().getId() + "\n"); break; } } }; static final int HANDLER_TEST = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "The main thread id = " + Thread.currentThread().getId() + "\n"); new myThread().start(); setContentView(R.layout.main); } class myThread extends Thread { public void run() { Message msg = new Message(); msg.what = HANDLER_TEST; h.sendMessage(msg); Log.d(TAG, "The worker thread id = " + Thread.currentThread().getId() + "\n"); } } }
以上代码中,Handler在主线程中建立后,子线程经过sendMessage()方法就能够将消息发送到主线程中,并在handleMessage()方法中处理