Android中的Looper,Handler和MessageQueue之间是什么关系?

编程入门 行业动态 更新时间:2024-10-10 21:21:34
本文介绍了Android中的Looper,Handler和MessageQueue之间是什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经查看了Looper,Handler和MessageQueue的官方Android文档/指南.但是我听不懂.我是android新手,对这些概念非常困惑.

I have checked the official Android documentation/guide for Looper, Handler and MessageQueue . But I couldn't get it. I am new to android, and got very confused with these concepts.

推荐答案

A Looper 是消息处理循环:它从 MessageQueue . Looper类通常与 HandlerThread (Thread的子类).

A Looper is a message handling loop: it reads and processes items from a MessageQueue. The Looper class is usually used in conjunction with a HandlerThread (a subclass of Thread).

Handler 是一种实用程序类,可促进交互使用Looper—主要是通过将消息和Runnable对象发布到线程的MessageQueue中.创建Handler时,它将绑定到特定的Looper(以及关联的线程和消息队列).

A Handler is a utility class that facilitates interacting with a Looper—mainly by posting messages and Runnable objects to the thread's MessageQueue. When a Handler is created, it is bound to a specific Looper (and associated thread and message queue).

在典型用法中,创建并启动HandlerThread,然后创建一个(或多个)对象,其他线程可以通过该对象与HandlerThread实例进行交互.必须在运行HandlerThread的同时创建Handler,尽管一旦创建就没有什么线程可以使用Handler的调度方法(post(Runnable)等)的限制

In typical usage, you create and start a HandlerThread, then create a Handler object (or objects) by which other threads can interact with the HandlerThread instance. The Handler must be created while running on the HandlerThread, although once created there is no restriction on what threads can use the Handler's scheduling methods (post(Runnable), etc.)

在创建应用程序实例之前,将Android应用程序中的主线程(也称为UI线程)设置为处理程序线程.

The main thread (a.k.a. UI thread) in an Android application is set up as a handler thread before your application instance is created.

除了类文档之外,所有这些此处.

Aside from the class docs, there's a nice discussion of all of this here.

P.S.上面提到的所有类都在软件包android.os中.

P.S. All the classes mentioned above are in the package android.os.

更多推荐

Android中的Looper,Handler和MessageQueue之间是什么关系?

本文发布于:2023-11-27 09:44:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637561.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   Looper   Android   MessageQueue   Handler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!