欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中的Looper对象详细介绍

程序员文章站 2023-11-08 14:43:52
java 官网对looper对象的说明: public class looperextends objectclass used to run a message loo...

java 官网对looper对象的说明:


public class looperextends object
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();
      }
  }

主要方法:

static void loop() :  run the message queue in this thread.
static void prepare() :  initialize the current thread as a looper.