Android中的Looper对象详细介绍

时间:2023-02-26 08:33:39 

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.

标签:Android,Looper
0
投稿

猜你喜欢

  • C#使用SQL Dataset数据集代码实例

    2023-02-24 08:23:07
  • Java中ResultSetMetaData 元数据的具体使用

    2021-06-25 12:38:13
  • Unity 按钮事件封装操作(EventTriggerListener)

    2022-07-08 10:07:08
  • Android利用ViewPager实现滑动广告板实例源码

    2021-12-17 18:48:15
  • 解决SpringMVC拦截器path路径的坑

    2023-03-24 17:02:01
  • 在Android设备上搭建Web服务器的方法

    2023-06-23 23:38:36
  • JAVA线程池原理实例详解

    2021-10-01 17:59:01
  • Android模拟器实现手机添加文件到sd卡的方法

    2022-01-18 08:38:27
  • IntelliJ IDEAx导出安卓(Android)apk文件图文教程

    2022-06-22 18:26:16
  • Android使用WebView实现全屏切换播放网页视频功能

    2021-10-19 18:21:16
  • Android中TabLayout结合ViewPager实现页面切换

    2022-11-01 18:24:10
  • C#微信公众号开发之消息处理

    2023-11-10 01:10:53
  • C# 屏蔽关键字的实现方法

    2023-10-10 15:15:50
  • C++实现TCP客户端及服务器Recv数据筛选处理详解

    2022-02-22 12:41:38
  • 深入理解C#之继承

    2022-02-25 07:25:44
  • C#使用读写锁三行代码简单解决多线程并发的问题

    2023-08-16 12:15:39
  • Java Swing中的文本框(JTextField)与文本区(JTextArea)使用实例

    2022-08-15 00:17:46
  • java递归菜单树转换成pojo对象

    2022-08-12 04:04:40
  • Java解析DICOM图之如何获得16进制数据详解

    2023-06-15 17:37:29
  • Java设计模式之工厂模式(Factory模式)介绍

    2021-10-15 11:13:15
  • asp之家 软件编程 m.aspxhome.com