Looper

来源:互联网 发布:中国报刊数据库 编辑:程序博客网 时间:2024/05/20 10:56

Looper用于在android线程中进行消息处理,默认情况下,一个线程并不和任何Looper绑定。当我们调用Looper.prepare()时,如果当前线程还没有和任何Looper绑定,那么将创建一个Looper让它和当前线程绑定。当我们调用Looper.loop()时,它将对当前线程所对应的Looper的消息进行处理,从消息队列里取消息,处理消息,一直循环直到对该Looper调用quit()函数。

       注意:Looper.loop()中是个while循环,只有对它所在线程的Looper调用了quit()函数,Looper.loop()函数才能完成,其后的代码才能得以运行。一个线程对应一个Looper,一个Looper对应一个消息队列MessageQueue

       对于Handler,其实只是把消息发送到其对应的Looper的消息队列MessageQueue中,最后的处理还是在Looper.loop()while循环中进行的。一个Looper可以用于构造多个Handler。因为Looper.loop()函数是个while循环,会让当前线程一直在那里处理进行循环,直到对该线程的Looper调用了quit()函数,所以,如果想对该Handler发送消息或添加Runnable以进行事务处理,要么在别的线程中进行,要么在该Handler在处理消息时或在Runnable()的run()函数中进行事务处理时进行。

       注意:Handler的构造函数Handler()Handler(Handler.Callback callback),虽然没有Looper参数,但是它实际上是通过Looper.myLooper()来获取当前线程中的Looper的。

Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理。handler其实可以看做是一个工具类,用来向消息队列中插入消息的。

(1) Looper类用来为一个线程开启一个消息循环。
    默认情况下android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息循环。)
    Looper对象通过MessageQueue来存放消息和事件。一个线程只能有一个Looper,对应一个MessageQueue

(2) 通常是通过Handler对象来与Looper进行交互的。Handler可看做是Looper的一个接口,用来向指定的Looper发送消息及定义处理方法。
    默认情况下Handler会与其被定义时所在线程的Looper绑定,比如,Handler在主线程中定义,那么它是与主线程的Looper绑定。
mainHandler = new Handler() 等价于new HandlerLooper.myLooper().
Looper.myLooper():获取当前进程的looper对象,类似的Looper.getMainLooper() 用于获取主线程的Looper对象。

(3) 在非主线程中直接new Handler()会报如下的错误:
E/AndroidRuntime( 6173): Uncaught handler: thread Thread-8 exiting due to uncaught exception
E/AndroidRuntime( 6173): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper

(4) Looper.loop(); Looper开始工作,从消息队列里取消息,处理消息。

    注意:写在Looper.loop()之后的代码不会被执行,这个函数内部应该是一个循环,当调用mHandler.getLooper().quit()后,loop才会中止,其后的代码才能得以运行。

(5) 基于以上知识,可实现主线程给子线程(非主线程)发送消息。

    把下面例子中的mHandler声明成类成员,在主线程通过mHandler发送消息即可。
  
    Android官方文档中Looper的介绍:
Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.

Most interaction with a message loop is through the Handler class.

This is a typical example of the implementation of a Looper thread, using the separation of prepare() and loop() to create an initial Handler to communicate with the 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();
      }
}