Android重要概念1-Handler类
2012-04-09 14:59
162 查看
Handler
A Handler allows you to send and process
/ message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.
Scheduling messages is accomplished with the
post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the
sendMessage versions allow you to enqueue a
When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement
timeouts, ticks, and other timing-based behavior.
When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your
own threads, and communicate back with the main application thread through a Handler. This is done by calling the same
post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.
A Handler allows you to send and process
Messageand Runnable objects associated with a thread's
MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread
/ message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.
Scheduling messages is accomplished with the
post(Runnable),
postAtTime(Runnable, long),
postDelayed(Runnable, long),
sendEmptyMessage(int),
sendMessage(Message),
sendMessageAtTime(Message, long), and
sendMessageDelayed(Message, long)methods. The
post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the
sendMessage versions allow you to enqueue a
Messageobject containing a bundle of data that will be processed by the Handler's
handleMessage(Message)method (requiring that you implement a subclass of Handler).
When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement
timeouts, ticks, and other timing-based behavior.
When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your
own threads, and communicate back with the main application thread through a Handler. This is done by calling the same
post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.
相关文章推荐
- Android重要概念3—App进程的生命周期
- Android重要概念3—Activity类
- AndroidStudio项目配置一个重要概念:源集
- Android关于屏幕适配的相关重要概念解析
- 学习Android前需要了解的几个重要概念
- 【内存优化】 Android 内存优化---概念、定位常用手段跟重要内存优化点
- Android架构宏观理解和Android四个重要组件概念
- Android重要概念2—Application类
- Android架构宏观理解和Android四个重要组件概念
- G-Sensor的一些概念,基于android
- [Elasticsearch] 聚合中的重要概念 - Buckets(桶)及Metrics(指标)
- 磁盘I/O相关的重要概念
- Cef重要概念
- Android Activity 生命周期和重要的相关函数(基础一)
- android ListView 几个重要属性
- Android ListView 九大重要属性详细分析
- Android ListView 几个重要属性
- android 开发 关于地图的几个概念
- Android组件的概念
- Java学习记录(记录一部分Java和C++区别和Java里一些比较重要的概念)--稍后会转到新浪博客