Android中的Looper,Handler和MessageQueue之间是什么关系? [英] What is the relationship between Looper, Handler and MessageQueue in Android?

查看:107
本文介绍了Android中的Looper,Handler和MessageQueue之间是什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了LooperHandlerMessageQueue的官方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 是消息处理循环:它从 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之间是什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆